| import { |
| disconnectAppSchema, |
| removeWhatsAppConnectionSchema, |
| updateAppSettingsSchema, |
| } from "@api/schemas/apps"; |
| import { createTRPCRouter, protectedProcedure } from "@api/trpc/init"; |
| import { |
| disconnectApp, |
| getApps, |
| removeWhatsAppConnection, |
| updateAppSettings, |
| updateAppSettingsBulk, |
| } from "@midday/db/queries"; |
| import { z } from "zod"; |
|
|
| export const appsRouter = createTRPCRouter({ |
| get: protectedProcedure.query(async ({ ctx: { db, teamId } }) => { |
| return getApps(db, teamId!); |
| }), |
|
|
| disconnect: protectedProcedure |
| .input(disconnectAppSchema) |
| .mutation(async ({ ctx: { db, teamId }, input }) => { |
| const { appId } = input; |
|
|
| return disconnectApp(db, { appId, teamId: teamId! }); |
| }), |
|
|
| update: protectedProcedure |
| .input(updateAppSettingsSchema) |
| .mutation(async ({ ctx: { db, teamId }, input }) => { |
| const { appId, option } = input; |
|
|
| return updateAppSettings(db, { |
| appId, |
| teamId: teamId!, |
| option, |
| }); |
| }), |
|
|
| updateSettings: protectedProcedure |
| .input( |
| z.object({ |
| appId: z.string(), |
| settings: z.array( |
| z.object({ |
| id: z.string(), |
| label: z.string().optional(), |
| description: z.string().optional(), |
| type: z.string().optional(), |
| required: z.boolean().optional(), |
| value: z.unknown(), |
| }), |
| ), |
| }), |
| ) |
| .mutation(async ({ ctx: { db, teamId }, input }) => { |
| const { appId, settings } = input; |
|
|
| return updateAppSettingsBulk(db, { |
| appId, |
| teamId: teamId!, |
| settings, |
| }); |
| }), |
|
|
| removeWhatsAppConnection: protectedProcedure |
| .input(removeWhatsAppConnectionSchema) |
| .mutation(async ({ ctx: { db, teamId }, input }) => { |
| const { phoneNumber } = input; |
|
|
| return removeWhatsAppConnection(db, { |
| teamId: teamId!, |
| phoneNumber, |
| }); |
| }), |
| }); |
|
|