@@ -186,26 +186,15 @@ export class UpsertTaskScheduleService extends BaseService {
186
186
} ) ;
187
187
188
188
// create the new instances
189
- let instances : InstanceWithEnvironment [ ] = [ ] ;
189
+ const newInstances : InstanceWithEnvironment [ ] = [ ] ;
190
+ const updatingInstances : InstanceWithEnvironment [ ] = [ ] ;
190
191
191
192
for ( const environmentId of options . environments ) {
192
193
const existingInstance = existingInstances . find ( ( i ) => i . environmentId === environmentId ) ;
193
194
194
195
if ( existingInstance ) {
195
- if ( ! existingInstance . active ) {
196
- // If the instance is not active, we need to activate it
197
- await tx . taskScheduleInstance . update ( {
198
- where : {
199
- id : existingInstance . id ,
200
- } ,
201
- data : {
202
- active : true ,
203
- } ,
204
- } ) ;
205
- }
206
-
207
196
// Update the existing instance
208
- instances . push ( { ... existingInstance , active : true } ) ;
197
+ updatingInstances . push ( existingInstance ) ;
209
198
} else {
210
199
// Create a new instance
211
200
const instance = await tx . taskScheduleInstance . create ( {
@@ -226,35 +215,53 @@ export class UpsertTaskScheduleService extends BaseService {
226
215
} ,
227
216
} ) ;
228
217
229
- instances . push ( instance ) ;
218
+ newInstances . push ( instance ) ;
230
219
}
231
220
}
232
221
233
222
// find the instances that need to be removed
234
- const instancesToDeactivate = existingInstances . filter (
223
+ const instancesToDeleted = existingInstances . filter (
235
224
( i ) => ! options . environments . includes ( i . environmentId )
236
225
) ;
237
226
238
- // deactivate the instances
239
- for ( const instance of instancesToDeactivate ) {
240
- await tx . taskScheduleInstance . update ( {
227
+ // delete the instances no longer selected
228
+ for ( const instance of instancesToDeleted ) {
229
+ await tx . taskScheduleInstance . delete ( {
241
230
where : {
242
231
id : instance . id ,
243
232
} ,
244
- data : {
245
- active : false ,
246
- } ,
247
233
} ) ;
248
234
}
249
235
250
- if ( scheduleHasChanged ) {
251
- const registerService = new RegisterNextTaskScheduleInstanceService ( tx ) ;
236
+ const registerService = new RegisterNextTaskScheduleInstanceService ( tx ) ;
237
+
238
+ for ( const instance of newInstances ) {
239
+ await registerService . call ( instance . id ) ;
240
+ }
252
241
253
- for ( const instance of existingInstances ) {
242
+ if ( scheduleHasChanged ) {
243
+ for ( const instance of updatingInstances ) {
254
244
await registerService . call ( instance . id ) ;
255
245
}
256
246
}
257
247
248
+ const instances = await tx . taskScheduleInstance . findMany ( {
249
+ where : {
250
+ taskScheduleId : scheduleRecord . id ,
251
+ } ,
252
+ include : {
253
+ environment : {
254
+ include : {
255
+ orgMember : {
256
+ include : {
257
+ user : true ,
258
+ } ,
259
+ } ,
260
+ } ,
261
+ } ,
262
+ } ,
263
+ } ) ;
264
+
258
265
return { scheduleRecord, instances } ;
259
266
}
260
267
0 commit comments