diff --git a/.yarn/install-state.gz b/.yarn/install-state.gz index e5bd509..c8ab534 100644 Binary files a/.yarn/install-state.gz and b/.yarn/install-state.gz differ diff --git a/packages/api/src/router/products.ts b/packages/api/src/router/products.ts index 4dfce9e..aaa24a4 100644 --- a/packages/api/src/router/products.ts +++ b/packages/api/src/router/products.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import type { SeatingStrategyName } from "@dotinc/bouncer-db"; import { createTRPCRouter, protectedProcedure } from "../trpc"; import { productConfig } from "@dotinc/bouncer-core"; +import { deployEventTypes } from "@dotinc/bouncer-events"; export const productsRouter = createTRPCRouter({ all: protectedProcedure @@ -118,6 +119,8 @@ export const productsRouter = createTRPCRouter({ .input(productConfig.omit({ owner_id: true })) .output(productConfig) .mutation(async ({ ctx, input }) => { + await deployEventTypes(); + console.log("creating configuration", input.id); const created = await ctx.prisma.$transaction([ ctx.prisma.seatingConfig.create({ @@ -165,6 +168,11 @@ export const productsRouter = createTRPCRouter({ }), ]); + await ctx.svix.application.create({ + uid: input.id, + name: input.product_name, + }); + return created[1]; }), }); diff --git a/packages/core/src/events/eventTypes.ts b/packages/core/src/events/eventTypes.ts index dc3736f..1bc59e4 100644 --- a/packages/core/src/events/eventTypes.ts +++ b/packages/core/src/events/eventTypes.ts @@ -33,6 +33,15 @@ export const seatReleasedEvent = z.object({ seat, }); +export const eventSchemas = { + no_seat_available: noSeatAvailableEvent, + low_seat_warning_level_reached: lowSeatWarningLevelReachedEvent, + seat_provided: seatProvidedEvent, + seat_reserved: seatReservedEvent, + seat_redeemed: seatRedeemedEvent, + seat_released: seatReleasedEvent, +}; + export type EventTypes = { no_seat_available: z.infer; low_seat_warning_level_reached: z.infer< diff --git a/packages/events/index.ts b/packages/events/index.ts index 5c859f2..f289390 100644 --- a/packages/events/index.ts +++ b/packages/events/index.ts @@ -1,2 +1,3 @@ export * from "./src"; export * from "./src/publish"; +export * from "./src/utils"; diff --git a/packages/events/package.json b/packages/events/package.json index d295622..5c8aad8 100644 --- a/packages/events/package.json +++ b/packages/events/package.json @@ -12,7 +12,8 @@ "dependencies": { "@dotinc/bouncer-core": "workspace:*", "svix": "^0.75.0", - "zod": "^3.20.2" + "zod": "^3.20.2", + "zod-to-json-schema": "^3.20.2" }, "devDependencies": { "typescript": "^4.9.4" diff --git a/packages/events/src/utils.ts b/packages/events/src/utils.ts new file mode 100644 index 0000000..7692a56 --- /dev/null +++ b/packages/events/src/utils.ts @@ -0,0 +1,40 @@ +import { svix } from "./index"; +import { eventSchemas, EventTypes } from "@dotinc/bouncer-core"; +import { zodToJsonSchema } from "zod-to-json-schema"; + +const missingTypes = (arr1: T[], arr2: T[]) => + arr1.filter((e) => !arr2.includes(e)); + +export const deployEventTypes = async () => { + const eventTypes = await svix.eventType.list({}); + const names = eventTypes.data.map((e) => e.name); + const missing = missingTypes( + Object.keys(eventSchemas) as (keyof EventTypes)[], + names as (keyof EventTypes)[] + ); + if (missing.length === 0) { + return; // all types are present + } + + const creates = []; + for (let i = 0; i < missing.length; i++) { + const eventType = missing[i]; + if (!eventType) continue; + + const eventSchema = zodToJsonSchema(eventSchemas[eventType], eventType); + + const jsonString = JSON.stringify(eventSchema); + + creates.push( + svix.eventType.create({ + name: eventType, + description: "", + schemas: { + default: jsonString, + }, + }) + ); + } + + await Promise.all(creates); +}; diff --git a/yarn.lock b/yarn.lock index fadf934..e0b00c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2029,6 +2029,7 @@ __metadata: svix: ^0.75.0 typescript: ^4.9.4 zod: ^3.20.2 + zod-to-json-schema: ^3.20.2 languageName: unknown linkType: soft