-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
packages/api/router/orgWebsite/mutation.locationLink.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 |
---|---|---|
@@ -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
10
packages/api/router/orgWebsite/mutation.locationLink.schema.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 |
---|---|---|
@@ -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
32
packages/api/router/orgWebsite/query.getLinkOptions.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 |
---|---|---|
@@ -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
6
packages/api/router/orgWebsite/query.getLinkOptions.schema.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 |
---|---|---|
@@ -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> |
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,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 |