diff --git a/packages/authorization-updater/src/readModelService.ts b/packages/authorization-updater/src/readModelService.ts index 97ad732e00..10c2967159 100644 --- a/packages/authorization-updater/src/readModelService.ts +++ b/packages/authorization-updater/src/readModelService.ts @@ -89,33 +89,22 @@ export function readModelServiceBuilder( eserviceId: EServiceId, consumerId: TenantId ): Promise { - const data = await agreements - .find( - { - "data.eserviceId": eserviceId, - "data.consumerId": consumerId, - "data.state": { - $in: [ - agreementState.active, - agreementState.archived, - agreementState.suspended, - ], - }, + const data = await agreements.findOne( + { + "data.eserviceId": eserviceId, + "data.consumerId": consumerId, + "data.state": { + $in: [ + agreementState.active, + agreementState.archived, + agreementState.suspended, + ], }, - { projection: { data: true } } - ) - .sort({ "data.createdAt": -1 }) - .limit(1) - .toArray(); - + }, + { projection: { data: true }, sort: { "data.createdAt": -1 } } + ); if (data) { - if (data.length > 1) { - throw genericInternalError( - `Too many agreements returned: data ${JSON.stringify(data)} ` - ); - } - - const result = Agreement.safeParse(data[0].data); + const result = Agreement.safeParse(data.data); if (!result.success) { throw genericInternalError(