Skip to content

Commit

Permalink
impersonated actions/mutations/notifications (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson authored Nov 12, 2024
1 parent 754d079 commit ef0a886
Show file tree
Hide file tree
Showing 24 changed files with 286 additions and 137 deletions.
21 changes: 12 additions & 9 deletions src/adverts/advert-mutations/claims/cancel-advert-claim.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { makeUser } from '../../../login'
import {
T,
createTestNotificationServices,
Expand All @@ -22,8 +21,8 @@ mutation Mutation(
`
describe('cancelAdvertClaim - reserved', () => {
it('removes all reservations (by user) from database', () => {
const advertReservationWasCancelled = jest.fn(async () => void 0)
const advertReservationWasCancelledOwner = jest.fn(async () => void 0)
const advertReservationWasCancelled = jest.fn(async () => undefined)
const advertReservationWasCancelledOwner = jest.fn(async () => undefined)
const notifications = createTestNotificationServices({
advertReservationWasCancelled,
advertReservationWasCancelledOwner,
Expand Down Expand Up @@ -106,7 +105,8 @@ describe('cancelAdvertClaim - reserved', () => {
'claims@user',
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
)

Expand All @@ -115,7 +115,8 @@ describe('cancelAdvertClaim - reserved', () => {
'some@owner',
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
)
}
Expand All @@ -125,8 +126,8 @@ describe('cancelAdvertClaim - reserved', () => {

describe('cancelAdvertClaim - collected', () => {
it('removes all reservations (by user) from database', () => {
const advertCollectWasCancelled = jest.fn(async () => void 0)
const advertCollectWasCancelledOwner = jest.fn(async () => void 0)
const advertCollectWasCancelled = jest.fn(async () => undefined)
const advertCollectWasCancelledOwner = jest.fn(async () => undefined)
const notifications = createTestNotificationServices({
advertCollectWasCancelled,
advertCollectWasCancelledOwner,
Expand Down Expand Up @@ -209,15 +210,17 @@ describe('cancelAdvertClaim - collected', () => {
'claims@user',
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
)
T('should have notified about the interesting event', () =>
expect(advertCollectWasCancelledOwner).toHaveBeenCalledWith(
'some@owner',
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
)
}
Expand Down
10 changes: 8 additions & 2 deletions src/adverts/advert-mutations/claims/cancel-advert-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const createCancelAdvertClaim =
Services,
'adverts' | 'notifications'
>): AdvertMutations['cancelAdvertClaim'] =>
(user, id, by, type) =>
(user, id, by, type, impersonate) =>
txBuilder<Advert>()
.load(() => adverts.getAdvert(user, id))
.validate(async (advert, { throwIf }) =>
Expand All @@ -38,7 +38,13 @@ export const createCancelAdvertClaim =
}

actions(patched =>
notifyClaimsWasCancelled(notifications, user, patched, claims)
notifyClaimsWasCancelled(
notifications,
user,
patched,
claims,
impersonate || null
)
)

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ describe('convertAdvertClaim', () => {
'claims@user',
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
})
T('should have notified about the interesting event', () => {
expect(advertWasCollectedOwner).toHaveBeenCalledWith(
'some@owner',
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
})
}
Expand Down
10 changes: 8 additions & 2 deletions src/adverts/advert-mutations/claims/convert-advert-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const createConvertAdvertClaim =
Services,
'adverts' | 'notifications'
>): AdvertMutations['convertAdvertClaim'] =>
(user, id, by, type, newType) =>
(user, id, by, type, newType, impersonate) =>
txBuilder<Advert>()
.load(() => adverts.getAdvert(user, id))
.validate(async (advert, { throwIf }) =>
Expand All @@ -47,7 +47,13 @@ export const createConvertAdvertClaim =
}))

actions(patched =>
notifyClaimsWas(notifications, user, patched, updatedClaims)
notifyClaimsWas(
notifications,
user,
patched,
updatedClaims,
impersonate || null
)
)
return {
...advert,
Expand Down
51 changes: 36 additions & 15 deletions src/adverts/advert-mutations/claims/notify-claims.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HaffaUser } from '../../../login/types'
import type { NotificationService } from '../../../notifications/types'
import type { ProfileInput } from '../../../profile/types'
import { normalizeAdvertClaims } from '../../advert-claims'
import type { Advert, AdvertClaim } from '../../types'
import { AdvertClaimType } from '../../types'
Expand All @@ -11,20 +12,28 @@ export const notifyClaimsWas = async (
notifications: NotificationService,
by: HaffaUser,
advert: Advert,
claims: AdvertClaim[]
claims: AdvertClaim[],
impersonate: Partial<ProfileInput> | null
) => {
const nc = normalizeAdvertClaims(claims)
await all(
nc
.filter(claim => claim.type === AdvertClaimType.reserved)
.map(claim =>
Promise.all([
notifications.advertWasReserved(claim.by, by, claim.quantity, advert),
notifications.advertWasReserved(
claim.by,
by,
claim.quantity,
advert,
impersonate
),
notifications.advertWasReservedOwner(
advert.createdBy,
by,
claim.quantity,
advert
advert,
impersonate
),
])
)
Expand All @@ -39,13 +48,15 @@ export const notifyClaimsWas = async (
claim.by,
by,
claim.quantity,
advert
advert,
impersonate
),
notifications.advertWasCollectedOwner(
advert.createdBy,
by,
claim.quantity,
advert
advert,
impersonate
),
])
)
Expand All @@ -56,7 +67,8 @@ export const notifyClaimsWasCancelled = async (
notifications: NotificationService,
by: HaffaUser,
advert: Advert,
claims: AdvertClaim[]
claims: AdvertClaim[],
impersonate: Partial<ProfileInput> | null
) => {
const nc = normalizeAdvertClaims(claims)
await all(
Expand All @@ -68,13 +80,15 @@ export const notifyClaimsWasCancelled = async (
claim.by,
by,
claim.quantity,
advert
advert,
impersonate
),
notifications.advertReservationWasCancelledOwner(
advert.createdBy,
by,
claim.quantity,
advert
advert,
impersonate
),
])
)
Expand All @@ -89,13 +103,15 @@ export const notifyClaimsWasCancelled = async (
claim.by,
by,
claim.quantity,
advert
advert,
impersonate
),
notifications.advertCollectWasCancelledOwner(
advert.createdBy,
by,
claim.quantity,
advert
advert,
impersonate
),
])
)
Expand All @@ -106,7 +122,8 @@ export const notifyClaimsWasRenewed = async (
notifications: NotificationService,
by: HaffaUser,
advert: Advert,
claims: AdvertClaim[]
claims: AdvertClaim[],
impersonate: Partial<ProfileInput> | null
) => {
const nc = normalizeAdvertClaims(claims)
await all(
Expand All @@ -118,13 +135,15 @@ export const notifyClaimsWasRenewed = async (
claim.by,
by,
claim.quantity,
advert
advert,
impersonate
),
notifications.advertReservationWasRenewedOwner(
advert.createdBy,
by,
claim.quantity,
advert
advert,
impersonate
),
])
)
Expand All @@ -139,13 +158,15 @@ export const notifyClaimsWasRenewed = async (
claim.by,
by,
claim.quantity,
advert
advert,
impersonate
),
notifications.advertCollectWasRenewedOwner(
advert.createdBy,
by,
claim.quantity,
advert
advert,
impersonate
),
])
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ describe('notifyExpiredClaims', () => {
'[email protected]',
expect.objectContaining(user),
2,
createTestAdvert()
createTestAdvert(),
null
)
expect(advertReservationWasCancelledOwner).toHaveBeenCalledWith(
adverts['advert-123'].createdBy,
expect.objectContaining(user),
2,
createTestAdvert()
createTestAdvert(),
null
)
}
)
Expand Down
6 changes: 4 additions & 2 deletions src/adverts/advert-mutations/claims/notify-expired-claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ export const createExpiredClaimsNotifier =
c.by,
user,
c.quantity,
advert
advert,
null
)
)
actions(() =>
notifications.advertReservationWasCancelledOwner(
advert.createdBy,
user,
c.quantity,
advert
advert,
null
)
)
actions(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ describe('renewAdvertClaim', () => {
'renew-user',
expect.objectContaining(user),
2,
adverts['advert-123']
adverts['advert-123'],
null
)
expect(advertCollectWasRenewedOwner).toHaveBeenCalledWith(
user.id,
expect.objectContaining(user),
2,
adverts['advert-123']
adverts['advert-123'],
null
)
})
}
Expand Down
5 changes: 3 additions & 2 deletions src/adverts/advert-mutations/claims/renew-advert-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const createRenewAdvertClaim =
Services,
'adverts' | 'notifications'
>): AdvertMutations['renewAdvertClaim'] =>
(user, id, by, type) =>
(user, id, by, type, impersonate) =>
txBuilder<Advert>()
.load(() => adverts.getAdvert(user, id))
.validate(async (advert, { throwIf }) =>
Expand All @@ -50,7 +50,8 @@ export const createRenewAdvertClaim =
notifications,
user,
patched,
patched.claims.filter(c => c.type === type && c.by === by)
patched.claims.filter(c => c.type === type && c.by === by),
impersonate || null
)
)

Expand Down
12 changes: 7 additions & 5 deletions src/adverts/advert-mutations/collecting/collect-advert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ mutation Mutation(

describe('collectAdvert', () => {
it('creates reservation claim', () => {
const advertWasCollected = jest.fn(async () => void 0)
const advertWasCollectedOwner = jest.fn(async () => void 0)
const advertWasCollected = jest.fn(async () => undefined)
const advertWasCollectedOwner = jest.fn(async () => undefined)
const notifications = createTestNotificationServices({
advertWasCollected,
advertWasCollectedOwner,
Expand Down Expand Up @@ -75,23 +75,25 @@ describe('collectAdvert', () => {
user.id,
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
)
T('should have notified about the interesting event', () =>
expect(advertWasCollectedOwner).toHaveBeenCalledWith(
'test@owner',
expect.objectContaining(user),
1,
adverts['advert-123']
adverts['advert-123'],
null
)
)
}
)
})

it('denies overcollects', () => {
const advertWasCollected = jest.fn(async () => void 0)
const advertWasCollected = jest.fn(async () => undefined)
const notifications = createTestNotificationServices({
advertWasCollected,
})
Expand Down
Loading

0 comments on commit ef0a886

Please sign in to comment.