Skip to content

Commit

Permalink
fix: update the options array of a sequence when creating options (No…
Browse files Browse the repository at this point in the history
  • Loading branch information
NoMercy235 committed Apr 11, 2020
1 parent 6a07895 commit 3986462
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/api/story/story.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ async function checkIfStoryCanPublish (story) {
async function getSequenceById (id) {
return await Sequence
.findOne({ _id: id })
.select('-scenePic')
.populate(['options']);
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const schema = new mongoose.Schema(

story: { type: String, ref: STORY },
chapter: { type: String, ref: CHAPTER, default: '' },
options: [{ type: String, ref: OPTION }],
options: [{ type: String, ref: OPTION, default: [] }],
},
{
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
Expand Down
8 changes: 6 additions & 2 deletions src/sockets/write-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ const handleSaveOptions = socket => async (options) => {
const optionsToCreate = options.filter(o => !o._id);
const optionsToUpdate = options.filter(o => !!o._id);

const createOperations = optionsToCreate.map(o => {
const createOperations = optionsToCreate.map(async o => {
const { _id, ...metadata } = o;
const dbOption = new Option(metadata);
return dbOption.save();
await dbOption.save();
const seq = await Sequence.findOne({ _id: o.sequence });
seq.options.push(dbOption._id);
await seq.save();
return dbOption;
});

const updateOperations = optionsToUpdate.map(o => {
Expand Down

0 comments on commit 3986462

Please sign in to comment.