Skip to content

Commit

Permalink
personalized return info
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson committed Jun 12, 2024
1 parent 70854ec commit 16c42e5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/adverts/advert-meta/advert-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const getAdvertMeta = (
isCollectedBySome,
isPicked: !!advert.pickedAt,
waitlistCount: advert.waitlist.length,
returnInfo: getClaimReturnInfo(advert.claims, advert.lendingPeriod),
returnInfo: getClaimReturnInfo(user, advert.claims, advert.lendingPeriod),
claims,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/adverts/advert-meta/tests/return-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ describe('getAdvertMeta::returnInfo', () => {
quantity: 0,
claims: [
createClaim({ at: '2024-01-05', quantity: 1 }),
createClaim({ at: '2024-01-01', quantity: 2 }),
createClaim({ at: '2024-01-01', quantity: 2, by: haffaUser.id }),
],
})
const meta = getAdvertMeta(advert, haffaUser)

expect(meta.returnInfo).toStrictEqual([
{ at: '2024-01-11T00:00:00.000Z', quantity: 2 },
{ at: '2024-01-15T00:00:00.000Z', quantity: 1 },
{ at: '2024-01-11T00:00:00.000Z', quantity: 2, isMine: true },
{ at: '2024-01-15T00:00:00.000Z', quantity: 1, isMine: false },
])
})
it('ignores claims of type reservation', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/adverts/advert-mutations/claims/mappers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { sortBy } from '../../../lib'
import { dateBuilder } from '../../../lib/date-builder'
import type { HaffaUser } from '../../../login/types'
import { AdvertClaimType } from '../../types'
import type { AdvertReturnInfo, AdvertClaim } from '../../types'

Expand Down Expand Up @@ -52,15 +53,17 @@ export const isClaimOverdue = (
}

export const getClaimReturnInfo = (
{ id }: HaffaUser,
claims: AdvertClaim[],
daysValid: number
): AdvertReturnInfo[] =>
daysValid > 0
? claims
.filter(({ type }) => type === AdvertClaimType.collected)
.map(({ at, quantity }) => ({
.map(({ at, by, quantity }) => ({
at: dateBuilder(at).addDays(daysValid).toISOString(),
quantity,
isMine: by === id,
}))
.sort(sortBy(c => c.at))
: []
Expand Down
5 changes: 3 additions & 2 deletions src/adverts/adverts.gql.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ export const advertsGqlSchema = /* GraphQL */ `
}
type AdvertReturnInfo {
at: String
quantity: Int
at: String!
quantity: Int!
isMine: Boolean!
}
type AdvertLocation {
Expand Down
1 change: 1 addition & 0 deletions src/adverts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface AdvertMetaClaim extends AdvertClaim {
export interface AdvertReturnInfo {
at: string
quantity: number
isMine: boolean
}
export interface AdvertMeta {
reservableQuantity: number
Expand Down

0 comments on commit 16c42e5

Please sign in to comment.