Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into dev
  • Loading branch information
jlarsson committed Feb 13, 2025
2 parents aedfe71 + 388e680 commit 47cc34e
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ PASSWORDLESS_FIXED_PINCODE=123456
#------------------------------------------------------------
# Automatically set advert to picked when collected
#PICK_ON_COLLECT=0

#------------------------------------------------------------
# Automatically set advert to un-picked when returned
#UNPICK_ON_RETURN=0
5 changes: 4 additions & 1 deletion src/adverts/advert-mutations/claims/cancel-advert-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export const createCancelAdvertClaim =
getAdvertMeta,
adverts,
notifications,
workflow: { unpickOnReturn },
}: Pick<
Services,
'getAdvertMeta' | 'adverts' | 'notifications'
'getAdvertMeta' | 'adverts' | 'notifications' | 'workflow'
>): AdvertMutations['cancelAdvertClaim'] =>
(user, id, by, type, impersonate) =>
txBuilder<Advert>()
Expand All @@ -45,9 +46,11 @@ export const createCancelAdvertClaim =
impersonate || null
)
)
const pickedAt = unpickOnReturn ? '' : advert.pickedAt

return {
...advert,
pickedAt,
claims: normalizeAdvertClaims(
advert.claims
.filter(c => !matchClaim(c))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ describe('convertAdvertClaim - picking', () => {
get pickOnCollect() {
return true
},
get unpickOnReturn() {
return false
},
}
const spy = jest.spyOn(workflow, 'pickOnCollect', 'get')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ describe('collectAdvert', () => {
get pickOnCollect() {
return true
},
get unpickOnReturn() {
return false
},
}
const spy = jest.spyOn(workflow, 'pickOnCollect', 'get')

Expand Down
65 changes: 65 additions & 0 deletions src/adverts/advert-mutations/collecting/return-advert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,69 @@ describe('returnAdvert', () => {
expect(result.status).toMatchObject(TxErrors.Unauthorized)
}
))
it('should set unpicked on return', () => {
const advertWasReturned = jest.fn(async () => undefined)
const advertWasReturnedOwner = jest.fn(async () => undefined)
const notifications = createTestNotificationServices({
advertWasReturned,
advertWasReturnedOwner,
})
const workflow = {
get pickOnCollect() {
return true
},
get unpickOnReturn() {
return true
},
}
const spy = jest.spyOn(workflow, 'unpickOnReturn', 'get')

return end2endTest(
{ services: { notifications, workflow } },
async ({ mappedGqlRequest, adverts, user, loginPolicies }) => {
// give us rights to collect
await loginPolicies.updateLoginPolicies([
{
emailPattern: user.id,
roles: ['canManageReturns'],
},
])

// eslint-disable-next-line no-param-reassign
adverts['advert-123'] = {
...createEmptyAdvert(),
id: 'advert-123',
createdBy: '[email protected]',
pickedAt: '2025-01-01T00:00:00:000Z',
quantity: 5,
claims: [
{
type: AdvertClaimType.collected,
by: '[email protected]',
quantity: 1,
at: new Date().toISOString(),
events: [],
},
],
}

const result = await mappedGqlRequest<AdvertMutationResult>(
'returnAdvert',
returnAdvertMutation,
{
id: 'advert-123',
}
)
expect(result.status).toBeNull()

T('should be updated in database', () =>
expect(adverts['advert-123'].pickedAt).toBe('')
)

T('should have checked configuration', () =>
expect(spy).toHaveBeenCalled()
)
}
)
})
})
6 changes: 5 additions & 1 deletion src/adverts/advert-mutations/collecting/return-advert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export const createReturnAdvert =
getAdvertMeta,
adverts,
notifications,
workflow: { unpickOnReturn },
}: Pick<
Services,
'getAdvertMeta' | 'adverts' | 'notifications'
'getAdvertMeta' | 'adverts' | 'notifications' | 'workflow'
>): AdvertMutations['returnAdvert'] =>
(user, id) =>
txBuilder<Advert>()
Expand All @@ -44,8 +45,11 @@ export const createReturnAdvert =
),
])
)
const pickedAt = unpickOnReturn ? '' : advert.pickedAt

return {
...advert,
pickedAt,
claims: normalizeAdvertClaims(
advert.claims.filter(
({ type }) => type !== AdvertClaimType.collected
Expand Down
1 change: 1 addition & 0 deletions src/adverts/adverts.gql.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const advertsGqlSchema = /* GraphQL */ `
externalId
tags
size
place
}
input AdvertFieldsFilterInput {
Expand Down
3 changes: 3 additions & 0 deletions src/test-utils/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export const createTestServices = (services: Partial<Services>): Services => {
get pickOnCollect() {
return false
},
get unpickOnReturn() {
return false
},
}
const getAdvertMeta = services.getAdvertMeta || createGetAdvertMeta()
const settings = services.settings || createInMemorySettingsService()
Expand Down
3 changes: 3 additions & 0 deletions src/workflow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export const createWorkflowServiceFromEnv = (): WorkflowService => ({
get pickOnCollect() {
return Number(getEnv('PICK_ON_COLLECT', { fallback: '0' })) === 1
},
get unpickOnReturn() {
return Number(getEnv('UNPICK_ON_RETURN', { fallback: '0' })) === 1
},
})
1 change: 1 addition & 0 deletions src/workflow/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface WorkflowService {
pickOnCollect: boolean
unpickOnReturn: boolean
}

0 comments on commit 47cc34e

Please sign in to comment.