-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
query.forLocationCard.handler.ts
69 lines (63 loc) · 1.91 KB
/
query.forLocationCard.handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { prisma } from '@weareinreach/db'
import { handleError } from '~api/lib/errorHandler'
import { globalWhere } from '~api/selects/global'
import { type TRPCHandlerParams } from '~api/types/handler'
import { type TForLocationCardSchema } from './query.forLocationCard.schema'
const forLocationCard = async ({ input, ctx }: TRPCHandlerParams<TForLocationCardSchema>) => {
try {
const { id, isEditMode } = input
const canSeeAll = isEditMode && !!ctx.session?.user?.permissions
const result = await prisma.orgLocation.findUniqueOrThrow({
where: {
id,
...(!canSeeAll && globalWhere.isPublic()),
},
select: {
id: true,
name: true,
street1: true,
street2: true,
city: true,
postCode: true,
latitude: true,
longitude: true,
notVisitable: true,
published: true,
deleted: true,
country: { select: { cca2: true } },
govDist: { select: { abbrev: true, tsKey: true, tsNs: true } },
phones: {
...(!canSeeAll && { where: { phone: globalWhere.isPublic() } }),
select: { phone: { select: { primary: true, number: true, country: { select: { cca2: true } } } } },
},
attributes: { select: { attribute: { select: { tsNs: true, tsKey: true, icon: true } } } },
services: {
select: {
service: {
select: {
services: { select: { tag: { select: { primaryCategory: { select: { tsKey: true } } } } } },
},
},
},
},
},
})
const transformed = {
...result,
country: result.country.cca2,
phones: result.phones.map(({ phone }) => ({ ...phone, country: phone.country.cca2 })),
attributes: result.attributes.map(({ attribute }) => attribute),
services: [
...new Set(
result.services.flatMap(({ service }) =>
service.services.map(({ tag }) => tag.primaryCategory.tsKey)
)
),
],
}
return transformed
} catch (err) {
return handleError(err)
}
}
export default forLocationCard