Skip to content

Commit 45eb432

Browse files
feat: Add endpoint to update state of multiple identities
1 parent 2db36c9 commit 45eb432

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/management/identities/identities.controller.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ export class IdentitiesController extends AbstractController {
235235
});
236236
}
237237

238+
@Patch('state')
239+
public async updateStateMany(
240+
@Res() res: Response,
241+
@Body()
242+
body: {
243+
originState: IdentityState;
244+
targetState: IdentityState;
245+
ids: Types.ObjectId[];
246+
},
247+
): Promise<Response> {
248+
const data = await this._service.updateStateMany(body);
249+
return res.status(HttpStatus.OK).json({
250+
statusCode: HttpStatus.OK,
251+
data,
252+
});
253+
}
254+
238255
@Delete(':_id([0-9a-fA-F]{24})')
239256
@ApiParam({ name: '_id', type: String })
240257
@ApiDeletedResponseDecorator(IdentitiesDto)

src/management/identities/identities.service.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ export class IdentitiesService extends AbstractServiceSchema {
185185
return updated;
186186
}
187187

188+
public async updateStateMany<T extends AbstractSchema | Document>(body: {
189+
ids: Types.ObjectId[];
190+
targetState: IdentityState;
191+
originState: IdentityState;
192+
}): Promise<ModifyResult<Query<T, T, any, T>>[]> {
193+
console.log(body);
194+
const identities = await this._model.find({ _id: { $in: body.ids } }).exec();
195+
if (identities.some((identity) => identity.state !== body.originState)) {
196+
throw new HttpException("Toutes les identités ne sont pas dans l'état attendu.", 400);
197+
}
198+
199+
if (identities.length === 0) {
200+
throw new HttpException('Aucune identité trouvée.', 404);
201+
}
202+
203+
const updated = await Promise.all(
204+
identities.map((identity) => {
205+
return this.updateState(identity._id, body.targetState, { rawResult: true });
206+
}),
207+
);
208+
209+
return updated;
210+
}
211+
188212
public async delete<T extends AbstractSchema | Document>(
189213
_id: Types.ObjectId | any,
190214
options?: QueryOptions<T> | null | undefined,

0 commit comments

Comments
 (0)