diff --git a/src/adverts/advert-mutations/claims/notify-expired-claims.spec.ts b/src/adverts/advert-mutations/claims/notify-expired-claims.spec.ts index 2d2df00..df19e66 100644 --- a/src/adverts/advert-mutations/claims/notify-expired-claims.spec.ts +++ b/src/adverts/advert-mutations/claims/notify-expired-claims.spec.ts @@ -1,5 +1,8 @@ import { createExpiredClaimsNotifier } from '.' -import { end2endTest } from '../../../test-utils' +import { + createTestNotificationServices, + end2endTest, +} from '../../../test-utils' import { createEmptyAdvert } from '../../mappers' import { AdvertClaimType } from '../../types' @@ -88,4 +91,65 @@ describe('notifyExpiredClaims', () => { ) expect(result.advert).toBeNull() })) + it('should call notification actions when a claim expired)', () => { + const advertReservationWasCancelled = jest.fn(async () => undefined) + const advertReservationWasCancelledOwner = jest.fn(async () => undefined) + const notifications = createTestNotificationServices({ + advertReservationWasCancelled, + advertReservationWasCancelledOwner, + }) + + return end2endTest( + { + services: { notifications }, + }, + async ({ user, adverts, services }) => { + const createTestAdvert = () => ({ + ...createEmptyAdvert(), + id: 'advert-123', + createdBy: user.id, + quantity: 50, + claims: [ + { + by: 'jane@doe1.se', + at: '2023-05-01T00:00:00.000Z', + quantity: 2, + type: AdvertClaimType.reserved, + events: [], + }, + { + by: 'jane@doe2.se', + at: '2024-05-01T00:00:00.000Z', + quantity: 4, + type: AdvertClaimType.reserved, + events: [], + }, + ], + }) + // eslint-disable-next-line no-param-reassign + adverts['advert-123'] = createTestAdvert() + + const notifyExpiredClaims = createExpiredClaimsNotifier(services) + + await notifyExpiredClaims( + user, + 'advert-123', + 1, + new Date('2024-05-01T00:00:00.000Z') + ) + expect(advertReservationWasCancelled).toHaveBeenCalledWith( + 'jane@doe1.se', + expect.objectContaining(user), + 2, + createTestAdvert() + ) + expect(advertReservationWasCancelledOwner).toHaveBeenCalledWith( + adverts['advert-123'].createdBy, + expect.objectContaining(user), + 2, + createTestAdvert() + ) + } + ) + }) }) diff --git a/src/adverts/advert-mutations/claims/notify-expired-claims.ts b/src/adverts/advert-mutations/claims/notify-expired-claims.ts index 96cca18..c3c96df 100644 --- a/src/adverts/advert-mutations/claims/notify-expired-claims.ts +++ b/src/adverts/advert-mutations/claims/notify-expired-claims.ts @@ -43,6 +43,14 @@ export const createExpiredClaimsNotifier = advert ) ) + actions(() => + notifications.advertReservationWasCancelledOwner( + advert.createdBy, + user, + c.quantity, + advert + ) + ) actions(() => syslog.write({ by: user.id,