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
34 changes: 30 additions & 4 deletions apps/web/src/server/api/routers/contacts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CampaignStatus, Prisma } from "@prisma/client";
import { TRPCError } from "@trpc/server";
import { z } from "zod";

import {
Expand Down Expand Up @@ -151,15 +152,40 @@ export const contactsRouter = createTRPCRouter({
subscribed: z.boolean().optional(),
}),
)
.mutation(async ({ input }) => {
.mutation(async ({ ctx: { contactBook }, input }) => {
const { contactId, ...contact } = input;
return contactService.updateContact(contactId, contact);
const updatedContact = await contactService.updateContactInContactBook(
contactId,
contactBook.id,
contact,
);

if (!updatedContact) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Contact not found",
});
}

return updatedContact;
}),

deleteContact: contactBookProcedure
.input(z.object({ contactId: z.string() }))
.mutation(async ({ input }) => {
return contactService.deleteContact(input.contactId);
.mutation(async ({ ctx: { contactBook }, input }) => {
const deletedContact = await contactService.deleteContactInContactBook(
input.contactId,
contactBook.id,
);

if (!deletedContact) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Contact not found",
});
}

return deletedContact;
}),

exportContacts: contactBookProcedure
Expand Down
18 changes: 14 additions & 4 deletions apps/web/src/server/public-api/api/contacts/delete-contact.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 { deleteContactInContactBook } from "~/server/service/contact-service";
import { getContactBook } from "../../api-utils";
import { UnsendApiError } from "../../api-error";

const route = createRoute({
method: "delete",
Expand Down Expand Up @@ -41,10 +41,20 @@ function deleteContactHandler(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = c.var.team;

await getContactBook(c, team.id);
const contactBook = await getContactBook(c, team.id);
const contactId = c.req.param("contactId");

await deleteContact(contactId);
const deletedContact = await deleteContactInContactBook(
contactId,
contactBook.id,
);

if (!deletedContact) {
throw new UnsendApiError({
code: "NOT_FOUND",
message: "Contact not found",
});
}

return c.json({ success: true });
});
Expand Down
19 changes: 15 additions & 4 deletions apps/web/src/server/public-api/api/contacts/update-contact.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createRoute, z } from "@hono/zod-openapi";
import { PublicAPIApp } from "~/server/public-api/hono";
import { getTeamFromToken } from "~/server/public-api/auth";
import { updateContact } from "~/server/service/contact-service";
import { updateContactInContactBook } from "~/server/service/contact-service";
import { getContactBook } from "../../api-utils";
import { UnsendApiError } from "../../api-error";

const route = createRoute({
method: "patch",
Expand Down Expand Up @@ -54,10 +54,21 @@ function updateContactInfo(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = c.var.team;

await getContactBook(c, team.id);
const contactBook = await getContactBook(c, team.id);
const contactId = c.req.param("contactId");

const contact = await updateContact(contactId, c.req.valid("json"));
const contact = await updateContactInContactBook(
contactId,
contactBook.id,
c.req.valid("json"),
);

if (!contact) {
throw new UnsendApiError({
code: "NOT_FOUND",
message: "Contact not found",
});
}

return c.json({ contactId: contact.id });
});
Expand Down
38 changes: 36 additions & 2 deletions apps/web/src/server/service/contact-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,32 @@ export async function addOrUpdateContact(
return createdContact;
}

export async function updateContact(
export async function getContactInContactBook(
contactId: string,
contactBookId: string,
) {
return db.contact.findFirst({
where: {
id: contactId,
contactBookId,
},
});
}

export async function updateContactInContactBook(
contactId: string,
contactBookId: string,
contact: Partial<ContactInput>,
) {
const existingContact = await getContactInContactBook(
contactId,
contactBookId,
);

if (!existingContact) {
return null;
}

return db.contact.update({
where: {
id: contactId,
Expand All @@ -75,7 +97,19 @@ export async function updateContact(
});
}

export async function deleteContact(contactId: string) {
export async function deleteContactInContactBook(
contactId: string,
contactBookId: string,
) {
const existingContact = await getContactInContactBook(
contactId,
contactBookId,
);

if (!existingContact) {
return null;
}

return db.contact.delete({
where: {
id: contactId,
Expand Down