Skip to content

Commit

Permalink
advert tags patching
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson committed May 24, 2024
1 parent 8742c9c commit 1d2231d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/adverts/advert-mutations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { createLeaveAdvertWaitlist } from './waitlist/leave-advert-waitlist'
import { createNotifyAdvertWaitlist } from './waitlist/notify-advert-waitlist'
import { createMarkAdvertAsPicked } from './picked/mark-advert-as-picked'
import { createMarkAdvertAsUnpicked } from './picked/mark-advert-as-unpicked'
import { createPatchAdvertTags } from './tags/patch-advert-tags'

export const createAdvertMutations = (
services: Pick<Services, 'adverts' | 'files' | 'notifications' | 'syslog'>
Expand All @@ -50,4 +51,5 @@ export const createAdvertMutations = (
notifyAdvertWaitlist: createNotifyAdvertWaitlist(services),
markAdvertAsPicked: createMarkAdvertAsPicked(services),
markAdvertAsUnpicked: createMarkAdvertAsUnpicked(services),
patchAdvertTags: createPatchAdvertTags(services),
})
32 changes: 32 additions & 0 deletions src/adverts/advert-mutations/tags/patch-advert-tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { exceptValues, uniqueBy } from '../../../lib'
import { TxErrors, txBuilder } from '../../../transactions'
import type { Services } from '../../../types'
import { getAdvertMeta } from '../../advert-meta'
import type { Advert, AdvertMutations } from '../../types'
import { mapTxResultToAdvertMutationResult } from '../mappers'

export const createPatchAdvertTags =
({
adverts,
}: Pick<
Services,
'adverts' | 'notifications'
>): AdvertMutations['patchAdvertTags'] =>
(user, id, { add, remove }) =>
txBuilder<Advert>()
.load(async () => adverts.getAdvert(user, id))
.validate((advert, { throwIf }) => {
throwIf(!getAdvertMeta(advert, user).canEdit, TxErrors.Unauthorized)
})
.patch(async advert => ({
...advert,
tags: [...advert.tags, ...add]
.filter(exceptValues(remove, v => v))
.filter(uniqueBy(v => v)),
}))
.verify(advert => advert)
.saveVersion(async (versionId, advert) =>
adverts.saveAdvertVersion(user, versionId, advert)
)
.run()
.then(result => mapTxResultToAdvertMutationResult(result))
6 changes: 6 additions & 0 deletions src/adverts/adverts-gql-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ export const createAdvertsGqlModule = (
.then(result =>
mapAdvertMutationResultToAdvertWithMetaMutationResult(user, result)
),
patchAdvertTags: async ({ ctx: { user }, args: { id, add, remove } }) =>
createAdvertMutations(services)
.patchAdvertTags(user, id, { add, remove })
.then(result =>
mapAdvertMutationResultToAdvertWithMetaMutationResult(user, result)
),
},
},
})
5 changes: 5 additions & 0 deletions src/adverts/adverts.gql.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const advertsGqlSchema = /* GraphQL */ `
leaveAdvertWaitlist(id: ID!): AdvertMutationResult
markAdvertAsPicked(id: ID!): AdvertMutationResult
markAdvertAsUnpicked(id: ID!): AdvertMutationResult
patchAdvertTags(
id: ID!
add: [String]!
remove: [String]!
): AdvertMutationResult
}
type AdvertMutationStatus {
Expand Down
5 changes: 5 additions & 0 deletions src/adverts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ export interface AdvertMutations {
user: HaffaUser,
id: string
) => Promise<AdvertMutationResult>
patchAdvertTags: (
user: HaffaUser,
id: string,
patch: { add: string; remove: string[] }
) => Promise<AdvertMutationResult>
}

interface AdvertStats {
Expand Down
9 changes: 9 additions & 0 deletions src/lib/except-values.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { exceptValues } from './except-values'

describe('exceptValues', () => {
it('just works', () => {
expect(
[1, 2, 3, 4, 5, 6].filter(exceptValues([1, 2, 3, 100, 200], v => v))
).toMatchObject([4, 5, 6])
})
})
14 changes: 14 additions & 0 deletions src/lib/except-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Func } from './types'

export const exceptValues = <T, V>(
values: V[],
mapValue: Func<T, V>
): Func<T, boolean> => {
const s = new Set<V>(values)
return (item: T) => {
if (s.has(mapValue(item))) {
return false
}
return true
}
}
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exceptValues } from './except-values'
import { mapValues } from './map-values'
import { obfuscate } from './obfuscate'
import { sortBy } from './sort-by'
Expand All @@ -16,6 +17,7 @@ export {
waitForAll,
uniqueBy,
sortBy,
exceptValues,
}

export const echo = <T>(value: T): T => {
Expand Down

0 comments on commit 1d2231d

Please sign in to comment.