add upsert and delete to contacts (#67)

This commit is contained in:
8x4
2024-09-05 01:18:21 +02:00
committed by GitHub
parent 48b2220ea3
commit 259937c60c
8 changed files with 436 additions and 105 deletions

View File

@@ -0,0 +1,53 @@
import { createRoute, z } from "@hono/zod-openapi";
import { PublicAPIApp } from "~/server/public-api/hono";
import { getTeamFromToken } from "~/server/public-api/auth";
import { deleteContact } from "~/server/service/contact-service";
import { getContactBook } from "../../api-utils";
const route = createRoute({
method: "delete",
path: "/v1/contactBooks/{contactBookId}/contacts/{contactId}",
request: {
params: z.object({
contactBookId: z.string().openapi({
param: {
name: "contactBookId",
in: "path",
},
example: "cuiwqdj74rygf74",
}),
contactId: z.string().openapi({
param: {
name: "contactId",
in: "path",
},
example: "cuiwqdj74rygf74",
}),
}),
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({ success: z.boolean() }),
},
},
description: "Contact deleted successfully",
},
},
});
function deleteContactHandler(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = await getTeamFromToken(c);
await getContactBook(c, team.id);
const contactId = c.req.param("contactId");
await deleteContact(contactId);
return c.json({ success: true });
});
}
export default deleteContactHandler;

View File

@@ -0,0 +1,65 @@
import { createRoute, z } from "@hono/zod-openapi";
import { PublicAPIApp } from "~/server/public-api/hono";
import { getTeamFromToken } from "~/server/public-api/auth";
import { addOrUpdateContact } from "~/server/service/contact-service";
import { getContactBook } from "../../api-utils";
const route = createRoute({
method: "put",
path: "/v1/contactBooks/{contactBookId}/contacts/{contactId}",
request: {
params: z.object({
contactBookId: z
.string()
.min(3)
.openapi({
param: {
name: "contactBookId",
in: "path",
},
example: "cuiwqdj74rygf74",
}),
}),
body: {
required: true,
content: {
"application/json": {
schema: z.object({
email: z.string(),
firstName: z.string().optional(),
lastName: z.string().optional(),
properties: z.record(z.string()).optional(),
subscribed: z.boolean().optional(),
}),
},
},
},
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({ contactId: z.string() }),
},
},
description: "Contact upserted successfully",
},
},
});
function upsertContact(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = await getTeamFromToken(c);
const contactBook = await getContactBook(c, team.id);
const contact = await addOrUpdateContact(
contactBook.id,
c.req.valid("json")
);
return c.json({ contactId: contact.id });
});
}
export default upsertContact;