generated from helsingborg-stad/gdi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
231 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/adverts/advert-mutations/claims/convert-advert-claim.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { | ||
T, | ||
createTestNotificationServices, | ||
end2endTest, | ||
} from '../../../test-utils' | ||
import { createEmptyAdvert } from '../../mappers' | ||
import type { AdvertMutationResult } from '../../types' | ||
import { AdvertClaimType } from '../../types' | ||
import { mutationProps } from '../test-utils/gql-test-definitions' | ||
|
||
const convertAdvertClaimMutation = /* GraphQL */ ` | ||
mutation Mutation( | ||
$id: ID!, | ||
$by: String!, | ||
$type: AdvertClaimType! | ||
$newType: AdvertClaimType! | ||
) { | ||
convertAdvertClaim(id: $id, by: $by, type: $type, newType: $newType) { | ||
${mutationProps} | ||
} | ||
} | ||
` | ||
describe('convertAdvertClaim', () => { | ||
it('can convert reservation to collect', () => { | ||
const advertWasCollected = jest.fn(async () => void 0) | ||
const notifications = createTestNotificationServices({ | ||
advertWasCollected, | ||
}) | ||
return end2endTest( | ||
{ | ||
services: { notifications }, | ||
}, | ||
async ({ mappedGqlRequest, adverts, user, loginPolicies }) => { | ||
// give us rights to handle claims | ||
await loginPolicies.updateLoginPolicies([ | ||
{ | ||
emailPattern: user.id, | ||
roles: ['canManageOwnAdvertsHistory'], | ||
}, | ||
]) | ||
// eslint-disable-next-line no-param-reassign | ||
adverts['advert-123'] = { | ||
...createEmptyAdvert(), | ||
id: 'advert-123', | ||
createdBy: user.id, | ||
quantity: 50, | ||
claims: [ | ||
{ | ||
by: 'someone I used to know', | ||
at: '', | ||
quantity: 2, | ||
type: AdvertClaimType.reserved, | ||
}, | ||
{ | ||
by: user.id, | ||
at: '', | ||
quantity: 1, | ||
type: AdvertClaimType.reserved, | ||
}, | ||
{ | ||
by: 'someone else', | ||
at: '', | ||
quantity: 1, | ||
type: AdvertClaimType.reserved, | ||
}, | ||
], | ||
} | ||
|
||
const result = await mappedGqlRequest<AdvertMutationResult>( | ||
'convertAdvertClaim', | ||
convertAdvertClaimMutation, | ||
{ | ||
id: 'advert-123', | ||
by: user.id, | ||
type: AdvertClaimType.reserved, | ||
newType: AdvertClaimType.collected, | ||
} | ||
) | ||
expect(result.status).toBeNull() | ||
|
||
T('claim should be updated in database', () => | ||
expect(adverts['advert-123'].claims).toMatchObject([ | ||
{ | ||
by: 'someone I used to know', | ||
at: '', | ||
quantity: 2, | ||
type: 'reserved', | ||
}, | ||
{ | ||
by: 'someone else', | ||
at: '', | ||
quantity: 1, | ||
type: 'reserved', | ||
}, | ||
{ | ||
by: user.id, | ||
// at: '', | ||
quantity: 1, | ||
type: AdvertClaimType.collected, | ||
}, | ||
]) | ||
) | ||
|
||
T('should have notified about the interesting event', () => | ||
expect(advertWasCollected).toHaveBeenCalledWith( | ||
user, | ||
1, | ||
adverts['advert-123'] | ||
) | ||
) | ||
} | ||
) | ||
}) | ||
}) |
76 changes: 76 additions & 0 deletions
76
src/adverts/advert-mutations/claims/convert-advert-claim.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { TxErrors, txBuilder } from '../../../transactions' | ||
import type { Services } from '../../../types' | ||
import { getAdvertMeta } from '../../advert-meta' | ||
import { AdvertClaimType } from '../../types' | ||
import type { AdvertClaim, Advert, AdvertMutations } from '../../types' | ||
import { mapTxResultToAdvertMutationResult } from '../mappers' | ||
import { | ||
verifyAll, | ||
verifyReservationLimits, | ||
verifyReservationsDoesNotExceedQuantity, | ||
verifyTypeIsReservation, | ||
} from '../verifiers' | ||
|
||
export const createConvertAdvertClaim = | ||
({ | ||
adverts, | ||
notifications, | ||
}: Pick< | ||
Services, | ||
'adverts' | 'notifications' | ||
>): AdvertMutations['convertAdvertClaim'] => | ||
(user, id, by, type, newType) => | ||
txBuilder<Advert>() | ||
.load(() => adverts.getAdvert(user, id)) | ||
.validate(async (advert, { throwIf }) => | ||
throwIf( | ||
!getAdvertMeta(advert, user).canManageClaims, | ||
TxErrors.Unauthorized | ||
) | ||
) | ||
.patch((advert, { actions }) => { | ||
const matchClaim = (c: AdvertClaim) => | ||
c.by === by && c.type === type && c.type !== newType | ||
const claims = advert.claims.filter(matchClaim) | ||
if (claims.length === 0) { | ||
return null | ||
} | ||
|
||
claims.forEach(claim => | ||
actions(patched => | ||
newType === AdvertClaimType.collected | ||
? notifications.advertWasCollected( | ||
{ id: by }, | ||
claim.quantity, | ||
patched | ||
) | ||
: Promise.resolve() | ||
) | ||
) | ||
return { | ||
...advert, | ||
claims: advert.claims | ||
.filter(c => !matchClaim(c)) | ||
.concat( | ||
claims.map(c => ({ | ||
...c, | ||
at: new Date().toISOString(), | ||
type: newType, | ||
})) | ||
) | ||
.filter(({ quantity }) => quantity > 0), | ||
} | ||
}) | ||
.verify((_, ctx) => | ||
verifyAll( | ||
ctx, | ||
verifyTypeIsReservation, | ||
verifyReservationLimits, | ||
verifyReservationsDoesNotExceedQuantity | ||
) | ||
) | ||
.saveVersion((versionId, advert) => | ||
adverts.saveAdvertVersion(user, versionId, advert) | ||
) | ||
.run() | ||
.then(mapTxResultToAdvertMutationResult) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters