diff --git a/packages/api/router/orgWebsite/mutation.locationLink.handler.ts b/packages/api/router/orgWebsite/mutation.locationLink.handler.ts new file mode 100644 index 0000000000..9b13608328 --- /dev/null +++ b/packages/api/router/orgWebsite/mutation.locationLink.handler.ts @@ -0,0 +1,39 @@ +import { getAuditedClient } from '@weareinreach/db' +import { handleError } from '~api/lib/errorHandler' +import { type TRPCHandlerParams } from '~api/types/handler' + +import { type TLocationLinkSchema } from './mutation.locationLink.schema' + +export const locationLink = async ({ ctx, input }: TRPCHandlerParams) => { + try { + const prisma = getAuditedClient(ctx.actorId) + const { action, orgWebsiteId, orgLocationId } = input + + switch (action) { + case 'link': { + const result = await prisma.orgLocationWebsite.create({ + data: { + orgWebsiteId, + orgLocationId, + active: true, + }, + }) + return result + } + case 'unlink': { + const result = await prisma.orgLocationWebsite.delete({ + where: { + orgLocationId_orgWebsiteId: { + orgWebsiteId, + orgLocationId, + }, + }, + }) + return result + } + } + } catch (error) { + handleError(error) + } +} +export default locationLink diff --git a/packages/api/router/orgWebsite/mutation.locationLink.schema.ts b/packages/api/router/orgWebsite/mutation.locationLink.schema.ts new file mode 100644 index 0000000000..d3c3c30c0b --- /dev/null +++ b/packages/api/router/orgWebsite/mutation.locationLink.schema.ts @@ -0,0 +1,10 @@ +import { z } from 'zod' + +import { prefixedId } from '~api/schemas/idPrefix' + +export const ZLocationLinkSchema = z.object({ + orgWebsiteId: prefixedId('orgWebsite'), + orgLocationId: prefixedId('orgLocation'), + action: z.enum(['link', 'unlink']), +}) +export type TLocationLinkSchema = z.infer diff --git a/packages/api/router/orgWebsite/query.getLinkOptions.handler.ts b/packages/api/router/orgWebsite/query.getLinkOptions.handler.ts new file mode 100644 index 0000000000..a977b55680 --- /dev/null +++ b/packages/api/router/orgWebsite/query.getLinkOptions.handler.ts @@ -0,0 +1,32 @@ +import { prisma } from '@weareinreach/db' +import { handleError } from '~api/lib/errorHandler' +import { type TRPCHandlerParams } from '~api/types/handler' + +import { type TGetLinkOptionsSchema } from './query.getLinkOptions.schema' + +export const getLinkOptions = async ({ input }: TRPCHandlerParams) => { + try { + const { slug, locationId } = input + const result = await prisma.orgWebsite.findMany({ + where: { + organization: { slug }, + locations: { every: { orgLocationId: { not: locationId } } }, + }, + select: { + id: true, + url: true, + published: true, + deleted: true, + description: { select: { tsKey: { select: { text: true } } } }, + }, + }) + const transformed = result.map(({ description, ...record }) => ({ + ...record, + description: description ? description.tsKey.text : null, + })) + return transformed + } catch (error) { + handleError(error) + } +} +export default getLinkOptions diff --git a/packages/api/router/orgWebsite/query.getLinkOptions.schema.ts b/packages/api/router/orgWebsite/query.getLinkOptions.schema.ts new file mode 100644 index 0000000000..70998fe615 --- /dev/null +++ b/packages/api/router/orgWebsite/query.getLinkOptions.schema.ts @@ -0,0 +1,6 @@ +import { z } from 'zod' + +import { prefixedId } from '~api/schemas/idPrefix' + +export const ZGetLinkOptionsSchema = z.object({ slug: z.string(), locationId: prefixedId('orgLocation') }) +export type TGetLinkOptionsSchema = z.infer diff --git a/packages/api/router/orgWebsite/schemas.ts b/packages/api/router/orgWebsite/schemas.ts index c0f965271c..8cfea38847 100644 --- a/packages/api/router/orgWebsite/schemas.ts +++ b/packages/api/router/orgWebsite/schemas.ts @@ -1,7 +1,9 @@ // codegen:start {preset: barrel, include: ./*.schema.ts} export * from './mutation.create.schema' +export * from './mutation.locationLink.schema' export * from './mutation.update.schema' export * from './query.forContactInfo.schema' export * from './query.forContactInfoEdit.schema' export * from './query.forEditDrawer.schema' +export * from './query.getLinkOptions.schema' // codegen:end