Skip to content

Commit

Permalink
feat: create svix consumer app and event types on create product
Browse files Browse the repository at this point in the history
  • Loading branch information
nadilas committed Feb 5, 2023
1 parent 77c06be commit 364d3d8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
Binary file modified .yarn/install-state.gz
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/api/src/router/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -165,6 +168,11 @@ export const productsRouter = createTRPCRouter({
}),
]);

await ctx.svix.application.create({
uid: input.id,
name: input.product_name,
});

return created[1];
}),
});
9 changes: 9 additions & 0 deletions packages/core/src/events/eventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof noSeatAvailableEvent>;
low_seat_warning_level_reached: z.infer<
Expand Down
1 change: 1 addition & 0 deletions packages/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./src";
export * from "./src/publish";
export * from "./src/utils";
3 changes: 2 additions & 1 deletion packages/events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions packages/events/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { svix } from "./index";
import { eventSchemas, EventTypes } from "@dotinc/bouncer-core";
import { zodToJsonSchema } from "zod-to-json-schema";

const missingTypes = <T>(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<keyof EventTypes>(
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);
};
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 364d3d8

Please sign in to comment.