Skip to content

Commit

Permalink
api route handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Feb 9, 2024
1 parent 6a21873 commit 3790a3c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/api/router/orgWebsite/mutation.locationLink.handler.ts
Original file line number Diff line number Diff line change
@@ -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<TLocationLinkSchema, 'protected'>) => {
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
10 changes: 10 additions & 0 deletions packages/api/router/orgWebsite/mutation.locationLink.schema.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ZLocationLinkSchema>
32 changes: 32 additions & 0 deletions packages/api/router/orgWebsite/query.getLinkOptions.handler.ts
Original file line number Diff line number Diff line change
@@ -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<TGetLinkOptionsSchema>) => {
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
6 changes: 6 additions & 0 deletions packages/api/router/orgWebsite/query.getLinkOptions.schema.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ZGetLinkOptionsSchema>
2 changes: 2 additions & 0 deletions packages/api/router/orgWebsite/schemas.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3790a3c

Please sign in to comment.