Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into IN-857-user-management
- Loading branch information
Showing
20 changed files
with
485 additions
and
203 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
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
93 changes: 52 additions & 41 deletions
93
packages/api/router/location/query.forLocationCard.handler.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 |
---|---|---|
@@ -1,56 +1,67 @@ | ||
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 }: TRPCHandlerParams<TForLocationCardSchema>) => { | ||
const result = await prisma.orgLocation.findUniqueOrThrow({ | ||
where: { | ||
id: input, | ||
...globalWhere.isPublic(), | ||
}, | ||
select: { | ||
id: true, | ||
name: true, | ||
street1: true, | ||
street2: true, | ||
city: true, | ||
postCode: true, | ||
latitude: true, | ||
longitude: true, | ||
notVisitable: true, | ||
country: { select: { cca2: true } }, | ||
govDist: { select: { abbrev: true, tsKey: true, tsNs: true } }, | ||
phones: { | ||
where: { phone: globalWhere.isPublic() }, | ||
select: { phone: { select: { primary: true, number: true, country: { select: { cca2: true } } } } }, | ||
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()), | ||
}, | ||
attributes: { select: { attribute: { select: { tsNs: true, tsKey: true, icon: true } } } }, | ||
services: { | ||
select: { | ||
service: { | ||
select: { | ||
services: { select: { tag: { select: { primaryCategory: { select: { tsKey: true } } } } } }, | ||
select: { | ||
id: true, | ||
name: true, | ||
street1: true, | ||
street2: true, | ||
city: true, | ||
postCode: true, | ||
latitude: true, | ||
longitude: true, | ||
notVisitable: 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)) | ||
), | ||
], | ||
} | ||
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 | ||
return transformed | ||
} catch (err) { | ||
return handleError(err) | ||
} | ||
} | ||
export default forLocationCard |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { type z } from 'zod' | ||
import { z } from 'zod' | ||
|
||
import { prefixedId } from '~api/schemas/idPrefix' | ||
|
||
export const ZForLocationCardSchema = prefixedId('orgLocation') | ||
export const ZForLocationCardSchema = z.object({ | ||
id: prefixedId('orgLocation'), | ||
isEditMode: z.boolean().optional().default(false), | ||
}) | ||
export type TForLocationCardSchema = z.infer<typeof ZForLocationCardSchema> |
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
Oops, something went wrong.