const {query} = require("../client") class Chapter { /** * @param {string} id * @param {string} title * @param {string} author * @param {string} source * @param {string | Date} createdDate * @param {string | Date} fictionalDate * @param {string | Date} editedDate */ constructor(id, title, author, source, createdDate, fictionalDate, editedDate) { this.id = id this.title = title this.author = author this.source = source this.createdDate = new Date(createdDate) this.fictionalDate = new Date(fictionalDate) this.editedDate = new Date(editedDate) } /** * Query the chapter's editable fields and update this object. This promise returns a boolean * whether there has been a change. * * @returns {Promise} `true` if the story was edited in the meantime */ update() { return query(` query UpdateChapter($id: String!) { update: chapter(id: $id) { title source fictionalDate editedDate } } `, {id: this.id}).then(({update}) => { const editedDate = new Date(udpate.editedDate) const wasEdited = editedDate.getTime() != this.editedDate.getTime() this.title = update.title this.source = update.source this.fictionalDate = new Date(update.fictionalDate) this.editedDate = editedDate return wasEdited }) } } module.exports = {Chapter}