Skip to content

Commit 19616e9

Browse files
committed
♻️ simulation upsert informations on event
1 parent eeac0ec commit 19616e9

File tree

5 files changed

+132
-117
lines changed

5 files changed

+132
-117
lines changed

src/features/groups/groups.repository.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const createGroupAndUser = async (
7171
const [participantDto] = participants || []
7272

7373
// For now no relation
74-
const [group, simulation] = await Promise.all([
74+
const [group, simulationCreation] = await Promise.all([
7575
// create group
7676
session.group.create({
7777
data: {
@@ -111,7 +111,11 @@ export const createGroupAndUser = async (
111111
: []),
112112
])
113113

114+
const { created, simulation, updated } = simulationCreation || {}
115+
114116
return {
117+
simulationUpdated: updated,
118+
simulationCreated: created,
115119
administrator,
116120
simulation,
117121
group: {
@@ -185,7 +189,7 @@ export const createParticipantAndUser = async (
185189

186190
// For now no relation
187191
const { id: simulationId } = simulationDto
188-
const [simulation, participant] = await Promise.all([
192+
const [simulationCreation, participant] = await Promise.all([
189193
createParticipantSimulation(
190194
{
191195
userId,
@@ -217,11 +221,19 @@ export const createParticipantAndUser = async (
217221
}),
218222
])
219223

224+
const {
225+
created: simulationCreated,
226+
simulation,
227+
updated: simulationUpdated,
228+
} = simulationCreation
229+
220230
return {
221231
participant: {
222232
...participant,
223233
simulation,
224234
},
235+
simulationUpdated,
236+
simulationCreated,
225237
created: !existingParticipant,
226238
updated: !!existingParticipant,
227239
}

src/features/groups/groups.service.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ export const createGroup = async ({
106106
groupDto: GroupCreateDto
107107
origin: string
108108
}) => {
109-
const { group, administrator, simulation } = await transaction((session) =>
110-
createGroupAndUser(groupDto, { session })
111-
)
109+
const {
110+
group,
111+
administrator,
112+
simulation,
113+
simulationUpdated,
114+
simulationCreated,
115+
} = await transaction((session) => createGroupAndUser(groupDto, { session }))
112116
const { participants } = group
113117

114118
const events = []
@@ -130,6 +134,8 @@ export const createGroup = async ({
130134
administrator,
131135
sendEmail: true,
132136
user: administrator,
137+
updated: simulationUpdated,
138+
created: simulationCreated,
133139
})
134140

135141
EventBus.emit(simulationUpsertedEvent)
@@ -180,9 +186,10 @@ export const createParticipant = async ({
180186
participantDto: ParticipantCreateDto
181187
}) => {
182188
try {
183-
const { participant, created } = await transaction((session) =>
184-
createParticipantAndUser(params, participantDto, { session })
185-
)
189+
const { participant, created, simulationCreated, simulationUpdated } =
190+
await transaction((session) =>
191+
createParticipantAndUser(params, participantDto, { session })
192+
)
186193
const {
187194
user,
188195
group,
@@ -198,6 +205,8 @@ export const createParticipant = async ({
198205
})
199206

200207
const simulationUpsertedEvent = new SimulationUpsertedEvent({
208+
created: simulationCreated,
209+
updated: simulationUpdated,
201210
sendEmail: created,
202211
administrator,
203212
simulation,

src/features/simulations/events/SimulationUpserted.event.ts

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,39 @@ import { EventBusEvent } from '../../../core/event-bus/event'
33
import type { ModelToDto } from '../../../types/types'
44
import type { SimulationCreateNewsletterList } from '../simulations.validator'
55

6-
type SimulationAttributes =
7-
| {
8-
origin: string
9-
user: Pick<User, 'id' | 'name' | 'email'>
10-
simulation: Pick<
11-
Simulation,
12-
'id' | 'progression' | 'actionChoices' | 'computedResults' | 'date'
13-
>
14-
group?: undefined
15-
administrator?: undefined
16-
organisation?: undefined
17-
newsletters: SimulationCreateNewsletterList
18-
sendEmail: boolean
19-
}
20-
| {
21-
origin: string
22-
user: Pick<User, 'id' | 'name' | 'email'>
23-
simulation: Pick<
24-
Simulation,
25-
'id' | 'progression' | 'actionChoices' | 'computedResults' | 'date'
26-
>
27-
group: Pick<Group, 'id' | 'name'>
28-
administrator: Pick<User, 'id'>
29-
organisation?: undefined
30-
newsletters?: undefined
31-
sendEmail: boolean
32-
}
33-
| {
34-
origin: string
35-
user: Pick<User, 'id' | 'name' | 'email'>
36-
simulation: Pick<
37-
Simulation,
38-
'id' | 'progression' | 'actionChoices' | 'computedResults' | 'date'
39-
>
40-
group?: undefined
41-
administrator?: undefined
42-
organisation: Pick<Organisation, 'name' | 'slug'>
43-
newsletters?: undefined
44-
sendEmail: boolean
45-
}
6+
type BaseSimulationUpsertedEventAttributes = {
7+
origin: string
8+
user: Pick<User, 'id' | 'name' | 'email'>
9+
simulation: Pick<
10+
Simulation,
11+
'id' | 'progression' | 'actionChoices' | 'computedResults' | 'date'
12+
>
13+
sendEmail: boolean
14+
created: boolean
15+
updated: boolean
16+
}
17+
18+
type SimulationAttributes = BaseSimulationUpsertedEventAttributes &
19+
(
20+
| {
21+
group?: undefined
22+
administrator?: undefined
23+
organisation?: undefined
24+
newsletters: SimulationCreateNewsletterList
25+
}
26+
| {
27+
group: Pick<Group, 'id' | 'name'>
28+
administrator: Pick<User, 'id'>
29+
organisation?: undefined
30+
newsletters?: undefined
31+
}
32+
| {
33+
group?: undefined
34+
administrator?: undefined
35+
organisation: Pick<Organisation, 'name' | 'slug'>
36+
newsletters?: undefined
37+
}
38+
)
4639

4740
export class SimulationUpsertedEvent extends EventBusEvent<SimulationAttributes> {
4841
name = 'SimulationUpsertedEvent'

src/features/simulations/simulations.repository.ts

Lines changed: 61 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const createUserSimulation = async (
5454
)
5555
}
5656

57-
export const createParticipantSimulation = <
57+
export const createParticipantSimulation = async <
5858
T extends
5959
Prisma.SimulationSelect = typeof defaultSimulationSelectionWithoutUser,
6060
>(
@@ -80,69 +80,63 @@ export const createParticipantSimulation = <
8080
},
8181
{ session }: { session: Session }
8282
) => {
83-
return session.simulation.upsert({
83+
const existingSimulation = await session.simulation.findUnique({
8484
where: {
8585
id,
8686
},
87-
create: {
88-
id,
89-
date,
90-
model,
91-
userId,
92-
situation,
93-
foldedSteps,
94-
progression,
95-
actionChoices,
96-
savedViaEmail,
97-
computedResults,
98-
...(!!additionalQuestionsAnswers?.length
99-
? {
100-
additionalQuestionsAnswers: {
101-
createMany: {
102-
data: additionalQuestionsAnswers.map(
103-
({ type, key, answer }) => ({
104-
type,
105-
key,
106-
answer,
107-
})
108-
),
109-
},
110-
},
111-
}
112-
: {}),
113-
},
114-
update: {
115-
id,
116-
date,
117-
model,
118-
userId,
119-
situation,
120-
foldedSteps,
121-
progression,
122-
actionChoices,
123-
savedViaEmail,
124-
computedResults,
125-
additionalQuestionsAnswers: {
126-
deleteMany: {
127-
simulationId: id,
128-
},
129-
...(!!additionalQuestionsAnswers?.length
130-
? {
131-
createMany: {
132-
data: additionalQuestionsAnswers.map(
133-
({ type, key, answer }) => ({
134-
type,
135-
key,
136-
answer,
137-
})
138-
),
139-
},
140-
}
141-
: {}),
142-
},
87+
select: {
88+
id: true,
14389
},
144-
select,
14590
})
91+
92+
const payload = {
93+
date,
94+
model,
95+
userId,
96+
situation,
97+
foldedSteps,
98+
progression,
99+
actionChoices,
100+
savedViaEmail,
101+
computedResults,
102+
...(!!additionalQuestionsAnswers?.length
103+
? {
104+
additionalQuestionsAnswers: {
105+
createMany: {
106+
data: additionalQuestionsAnswers.map(({ type, key, answer }) => ({
107+
type,
108+
key,
109+
answer,
110+
})),
111+
},
112+
},
113+
}
114+
: {}),
115+
}
116+
117+
const simulation = !!existingSimulation
118+
? await session.simulation.update({
119+
where: {
120+
id,
121+
},
122+
data: {
123+
...payload,
124+
},
125+
select,
126+
})
127+
: await session.simulation.create({
128+
data: {
129+
id,
130+
...payload,
131+
},
132+
select,
133+
})
134+
135+
return {
136+
simulation,
137+
created: !existingSimulation,
138+
updated: !!existingSimulation,
139+
}
146140
}
147141

148142
export const fetchUserSimulations = (
@@ -211,11 +205,11 @@ export const createPollUserSimulation = async (
211205
select: { id: true },
212206
})
213207

214-
const { id: simulationId } = await createUserSimulation(
215-
params,
216-
simulationDto,
217-
{ session }
218-
)
208+
const {
209+
simulation: { id: simulationId },
210+
created: simulationCreated,
211+
updated: simulationUpdated,
212+
} = await createUserSimulation(params, simulationDto, { session })
219213

220214
const relation = {
221215
pollId,
@@ -254,8 +248,10 @@ export const createPollUserSimulation = async (
254248
}
255249

256250
return {
257-
simulation,
258251
poll,
252+
simulation,
253+
simulationCreated,
254+
simulationUpdated,
259255
created: !existingParticipation,
260256
updated: !!existingParticipation,
261257
}

src/features/simulations/simulations.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const createSimulation = async ({
7171
params: UserParams
7272
origin: string
7373
}) => {
74-
const simulation = await transaction((session) =>
74+
const { simulation, created, updated } = await transaction((session) =>
7575
createUserSimulation(params, simulationDto, { session })
7676
)
7777
const { user } = simulation
@@ -80,6 +80,8 @@ export const createSimulation = async ({
8080
newsletters,
8181
simulation,
8282
sendEmail,
83+
created,
84+
updated,
8385
origin,
8486
user,
8587
})
@@ -126,9 +128,10 @@ export const createPollSimulation = async ({
126128
simulationDto: SimulationCreateDto
127129
}) => {
128130
try {
129-
const { poll, simulation, created } = await transaction((session) =>
130-
createPollUserSimulation(params, simulationDto, { session })
131-
)
131+
const { poll, simulation, simulationCreated, simulationUpdated, created } =
132+
await transaction((session) =>
133+
createPollUserSimulation(params, simulationDto, { session })
134+
)
132135
const { user } = simulation
133136
const { organisation } = poll
134137

@@ -138,6 +141,8 @@ export const createPollSimulation = async ({
138141
})
139142

140143
const simulationUpsertedEvent = new SimulationUpsertedEvent({
144+
created: simulationCreated,
145+
updated: simulationUpdated,
141146
sendEmail: created,
142147
organisation,
143148
simulation,

0 commit comments

Comments
 (0)