Skip to content

Commit 3be7ff3

Browse files
♻️ refactor create / delete flag functions (#345)
Move add / delete certified flag prisma mutation directly inside the functions that add / remove the flag in the history and in Algolia
1 parent c737e24 commit 3be7ff3

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

server/src/helpers/certified.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ const deleteCertifedFlagIfNoLongerApplicable = async (history, node, tags, ctx)
1313
specialties && Boolean(specialties.find(specialty => tags.includes(specialty.id)))
1414

1515
if (!isUserSpecialist) {
16-
await ctx.prisma.mutation.deleteFlag({
17-
where: {
18-
id: certifiedFlag.id
19-
}
20-
})
21-
await deleteFlagAndUpdateHistoryAndAlgolia(history, type, ctx, certifiedFlag.id)
16+
await deleteFlagAndUpdateHistoryAndAlgolia(history, type, ctx, node.id, certifiedFlag.id)
2217
}
2318
}
2419
}
@@ -30,19 +25,9 @@ const refreshCertifiedFlag = async (history, answer, user, ctx) => {
3025
const isUserSpecialist = Boolean(specialties.find(specialty => tags.includes(specialty.id)))
3126

3227
if (isUserSpecialist && !certifiedFlag) {
33-
await ctx.prisma.mutation.createFlag({
34-
data: {
35-
type: type,
36-
node: { connect: { id: answer.node.id } },
37-
user: { connect: { id: user.id } }
38-
}
39-
})
40-
await createFlagAndUpdateHistoryAndAlgolia(history, type, ctx, answer.node.id)
28+
await createFlagAndUpdateHistoryAndAlgolia(history, type, ctx, answer.node.id, user.id)
4129
} else if (!isUserSpecialist && certifiedFlag) {
42-
await ctx.prisma.mutation.deleteFlag({
43-
where: { id: certifiedFlag.id }
44-
})
45-
await deleteFlagAndUpdateHistoryAndAlgolia(history, type, ctx, answer.node.id)
30+
await deleteFlagAndUpdateHistoryAndAlgolia(history, type, ctx, answer.node.id, certifiedFlag.id)
4631
}
4732
}
4833

server/src/helpers/updateHistoryAndAlgolia.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const { algolia } = require('../integrations')
22

3-
const createFlagAndUpdateHistoryAndAlgolia = async (history, type, ctx, nodeId) => {
3+
const createFlagAndUpdateHistoryAndAlgolia = async (history, type, ctx, nodeId, userId) => {
4+
await ctx.prisma.mutation.createFlag({
5+
data: {
6+
type: type,
7+
node: { connect: { id: nodeId } },
8+
user: { connect: { id: userId } }
9+
}
10+
})
411
await history.push(ctx, {
512
action: 'CREATED',
613
model: 'Flag',
@@ -13,7 +20,12 @@ const createFlagAndUpdateHistoryAndAlgolia = async (history, type, ctx, nodeId)
1320
algolia.updateNode(ctx, nodeId)
1421
}
1522

16-
const deleteFlagAndUpdateHistoryAndAlgolia = async (history, type, ctx, nodeId) => {
23+
const deleteFlagAndUpdateHistoryAndAlgolia = async (history, type, ctx, nodeId, flagId) => {
24+
await ctx.prisma.mutation.deleteFlag({
25+
where: {
26+
id: flagId
27+
}
28+
})
1729
await history.push(ctx, {
1830
action: 'DELETED',
1931
model: 'Flag',

server/src/resolvers/flag.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@ module.exports = {
1111
const flag = await ctx.prisma.exists.Flag({ node: { id: nodeId }, type })
1212

1313
if (!flag) {
14-
await ctx.prisma.mutation.createFlag({
15-
data: {
16-
type,
17-
node: { connect: { id: nodeId } },
18-
user: { connect: { id: ctxUser(ctx).id } }
19-
}
20-
})
21-
22-
await createFlagAndUpdateHistoryAndAlgolia(history, type, ctx, nodeId)
14+
await createFlagAndUpdateHistoryAndAlgolia(history, type, ctx, nodeId, ctxUser(ctx).id)
2315
}
2416

2517
return ctx.prisma.query.zNode({ where: { id: nodeId } }, info)
@@ -33,11 +25,7 @@ module.exports = {
3325
)
3426

3527
if (flags) {
36-
await ctx.prisma.mutation.deleteFlag({
37-
where: { id: flags[0].id }
38-
})
39-
40-
await deleteFlagAndUpdateHistoryAndAlgolia(history, type, ctx, nodeId)
28+
await deleteFlagAndUpdateHistoryAndAlgolia(history, type, ctx, nodeId, flags[0].id)
4129
}
4230

4331
return ctx.prisma.query.zNode({ where: { id: nodeId } }, info)

0 commit comments

Comments
 (0)