Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions core/lib/contracts/resources/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,30 @@ const contract = initContract();

const PubFieldsSchema = z.any();

const PubPostSchema = z.object({
pubTypeId: z.string(),
pubFields: PubFieldsSchema,
});

export type PubFieldsResponse = z.infer<typeof PubFieldsSchema>;
export type PubPostBody = z.infer<typeof PubPostSchema>;

export const pubApi = contract.router({
getPubFields: {
createPub: {
method: "POST",
path: "/:instanceId/pub",
summary: "Creates a new pub",
description: "A way to create a new pub",
body: PubPostSchema,
pathParams: z.object({
instanceId: z.string(),
}),
responses: {
200: PubFieldsSchema,
404: z.object({ message: z.string() }),
},
},
getPub: {
method: "GET",
path: "/:instanceId/pub/:pubId",
summary: "Get all pubs",
Expand All @@ -21,11 +41,11 @@ export const pubApi = contract.router({
200: z.array(PubFieldsSchema),
},
},
putPubFields: {
updatePub: {
method: "PATCH",
path: "/:instanceId/pub/:pubId",
summary: "Adds field(s) to a pub",
description: "A way to add a field to an existing pub",
description: "A way to update a field for an existing pub",
body: PubFieldsSchema,
pathParams: z.object({
pubId: z.string(),
Expand Down
6 changes: 2 additions & 4 deletions core/lib/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import { getPub, updatePub } from "./pub";
import { getMembers } from "./autosuggest";

export { getPub, updatePub, getMembers };
export * from "./pub";
export * from "./autosuggest";
84 changes: 72 additions & 12 deletions core/lib/server/pub.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import prisma from "~/prisma/db";
import { PubPostBody } from "~/lib/contracts/resources/pub";

export const getPubFields = async (pubId: string) => {
const fields = await prisma.pubValue.findMany({
Expand All @@ -22,33 +23,92 @@ export const getPubFields = async (pubId: string) => {
}, {});
};

export const getPub = async (pubId: string) => {
const pub = await getPubFields(pubId);
return pub;
};
export class NotFoundError extends Error {}

const InstanceNotFoundError = new NotFoundError("Integration instance not found");
const PubTypeNotFoundError = new NotFoundError("PubType not found");
const PubNotFoundError = new NotFoundError("Pub not found");
const PubFieldNamesNotFoundError = new NotFoundError("Pub fields not found");

export const updatePub = async (pubId: string, fields: any) => {
const fieldNames = Object.keys(fields);
const getPubValues = async (pubFields: any, pubTypeId?: string) => {
const fieldNames = Object.keys(pubFields);

const fieldIds = await prisma.pubField.findMany({
where: {
name: {
in: fieldNames,
},
},
select: {
id: true,
name: true,
pubTypes: {
some: {
id: pubTypeId,
},
},
},
});

const newValues = fieldIds.map((field) => {
if (!fieldIds) {
throw PubFieldNamesNotFoundError;
}

const values = fieldIds.map((field) => {
return {
fieldId: field.id,
value: fields[field.name],
value: pubFields[field.name],
};
});

return values;
};

export const createPub = async (instanceId: string, body: PubPostBody) => {
const { pubTypeId, pubFields } = body;

const [instance, pubType] = await Promise.all([
prisma.integrationInstance.findUnique({
where: { id: instanceId },
}),
prisma.pubType.findUnique({
where: { id: pubTypeId },
}),
]);

if (!instance) {
throw InstanceNotFoundError;
}

if (!pubType) {
throw PubTypeNotFoundError;
}

const pubValues = await getPubValues(pubFields, pubType.id);

const pub = await prisma.pub.create({
data: {
pubTypeId: pubType.id,
communityId: instance.communityId,
values: {
createMany: {
data: pubValues,
},
},
},
});

if (!pub) {
throw PubNotFoundError;
}

return pub;
};

export const getPub = async (pubId: string) => {
const pub = await getPubFields(pubId);
return pub;
};

export const updatePub = async (pubId: string, pubFields: any) => {
const newValues = await getPubValues(pubFields);

await prisma.pub.update({
where: { id: pubId },
include: {
Expand Down
25 changes: 21 additions & 4 deletions core/pages/api/[...ts-rest].ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import { createNextRoute, createNextRouter } from "@ts-rest/next";
import { api } from "~/lib/contracts";
import { getPub, getMembers, updatePub } from "~/lib/server";
import { getPub, getMembers, updatePub, createPub, NotFoundError } from "~/lib/server";

// TODOD: verify pub belongs to integrationInstance
// TODO: verify pub belongs to integrationInstance
const pubRouter = createNextRoute(api.pub, {
getPubFields: async ({ params }) => {
createPub: async ({ params, body }) => {
try {
const pub = await createPub(params.instanceId, body);
return { status: 200, body: pub };
} catch (error) {
if (error instanceof NotFoundError) {
return {
status: 404,
body: { message: error.message },
};
}
return {
status: 500,
body: { message: "Internal Server Error" },
};
}
},
getPub: async ({ params }) => {
const pubFieldValuePairs = await getPub(params.pubId);
return {
status: 200,
body: pubFieldValuePairs,
};
},
putPubFields: async ({ params, body }) => {
updatePub: async ({ params, body }) => {
const updatedPub = await updatePub(params.pubId, body);
return {
status: 200,
Expand Down