Skip to content

Commit

Permalink
refactor(CreateMediaArticle): adjust async operations
Browse files Browse the repository at this point in the history
- Each operation just await its dependencies
- CraeteMediaArticle returns when all operations settles
  • Loading branch information
MrOrz committed Sep 28, 2023
1 parent 016e063 commit 6ee0c01
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions src/graphql/mutations/CreateMediaArticle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import { GraphQLString, GraphQLNonNull, GraphQLBoolean } from 'graphql';
import sharp from 'sharp';
import { MediaType, variants } from '@cofacts/media-manager';
import mediaManager, {
Expand Down Expand Up @@ -206,11 +206,8 @@ export default {
articleType: { type: new GraphQLNonNull(ArticleTypeEnum) },
reference: { type: new GraphQLNonNull(ArticleReferenceInput) },
reason: {
// FIXME: Change to required field after LINE bot is implemented
// type: new GraphQLNonNull(GraphQLString),
type: GraphQLString,
description:
'The reason why the user want to submit this article. Mandatory for 1st sender',
description: 'The reason why the user want to submit this article',
},
},
async resolve(
Expand All @@ -225,44 +222,45 @@ export default {
articleType,
});

const articleId = await createNewMediaArticle({
const aritcleIdPromise = createNewMediaArticle({
mediaEntry,
articleType,
reference,
user,
});

// Write AI transcript to article & ydoc without blocking
getAIResponse({
const aiResponsePromise = getAIResponse({
type: 'TRANSCRIPT',
docId: mediaEntry.id,
})
.then(aiResponse => {
if (!aiResponse) {
throw new Error('AI transcript not found');
}
return writeAITranscript(articleId, aiResponse.text);
})
.then(() => {
console.log(
`[CreateMediaArticle] AI transcript for ${
mediaEntry.id
} applied to article ${articleId}`
);
})
.catch(e =>
console.warn(
`[CreateMediaArticle] ${mediaEntry.id} (article ${articleId})`,
e
)
);

await createOrUpdateReplyRequest({
articleId,
user,
reason,
});

return { id: articleId };
await Promise.all([
// Update reply request
aritcleIdPromise.then(articleId =>
createOrUpdateReplyRequest({
articleId,
user,
reason,
})
),

// Write AI transcript to article & ydoc
Promise.all([aritcleIdPromise, aiResponsePromise])
.then(([articleId, aiResponse]) => {
if (!aiResponse) {
throw new Error('AI transcript not found');
}
return writeAITranscript(articleId, aiResponse.text);
})
.then(() => {
console.log(
`[CreateMediaArticle] AI transcript for ${mediaEntry.id} applied`
);
})
// It's OK to fail this promise, just log as warning
.catch(e => console.warn(`[CreateMediaArticle] ${mediaEntry.id}:`, e)),
]);

return { id: await aritcleIdPromise };
},
};

0 comments on commit 6ee0c01

Please sign in to comment.