feat: add contactBooks to sdk, add delete campaign public endpoint (#352)

* feat: add contactBooks to sdk, add delete campaign public endpoint

* fix: pr review notes

* refactor: pr feedback

* feat: bulk delete/create contacts

* refactor: rename a few methods for consistency

* refactor: update openapi docs based on pr feedback

* refactor: update open api docs, based on pr feedback

* fix: delete campaign security issue

* refactor: delete campaign requires team id (from context)

* fix: enums
This commit is contained in:
Dave Stockley
2026-03-03 20:10:43 +00:00
committed by GitHub
parent 73416dc481
commit 7428a1fbfa
22 changed files with 1365 additions and 218 deletions
+177 -164
View File
@@ -11,188 +11,201 @@ import * as contactService from "~/server/service/contact-service";
import * as contactBookService from "~/server/service/contact-book-service";
export const contactsRouter = createTRPCRouter({
getContactBooks: teamProcedure
.input(z.object({ search: z.string().optional() }))
.query(async ({ ctx: { team }, input }) => {
return contactBookService.getContactBooks(team.id, input.search);
}),
getContactBooks: teamProcedure
.input(z.object({ search: z.string().optional() }))
.query(async ({ ctx: { team }, input }) => {
return contactBookService.getContactBooks(team.id, input.search);
}),
createContactBook: teamProcedure
.input(
z.object({
name: z.string(),
}),
)
.mutation(async ({ ctx: { team }, input }) => {
const { name } = input;
return contactBookService.createContactBook(team.id, name);
}),
createContactBook: teamProcedure
.input(
z.object({
name: z.string(),
}),
)
.mutation(async ({ ctx: { team }, input }) => {
const { name } = input;
return contactBookService.createContactBook(team.id, name);
}),
getContactBookDetails: contactBookProcedure.query(
async ({ ctx: { contactBook } }) => {
const { totalContacts, unsubscribedContacts, campaigns } =
await contactBookService.getContactBookDetails(contactBook.id);
getContactBookDetails: contactBookProcedure.query(
async ({ ctx: { contactBook } }) => {
const { totalContacts, unsubscribedContacts, campaigns } =
await contactBookService.getContactBookDetails(contactBook.id);
return {
...contactBook,
totalContacts,
unsubscribedContacts,
campaigns,
};
},
),
return {
...contactBook,
totalContacts,
unsubscribedContacts,
campaigns,
};
},
),
updateContactBook: contactBookProcedure
.input(
z.object({
contactBookId: z.string(),
name: z.string().optional(),
properties: z.record(z.string()).optional(),
emoji: z.string().optional(),
doubleOptInEnabled: z.boolean().optional(),
doubleOptInFrom: z.string().nullable().optional(),
doubleOptInSubject: z.string().optional(),
doubleOptInContent: z.string().optional(),
}),
)
.mutation(async ({ ctx: { contactBook }, input }) => {
const { contactBookId, ...data } = input;
return contactBookService.updateContactBook(contactBook.id, data);
}),
updateContactBook: contactBookProcedure
.input(
z.object({
contactBookId: z.string(),
name: z.string().optional(),
properties: z.record(z.string()).optional(),
emoji: z.string().optional(),
doubleOptInEnabled: z.boolean().optional(),
doubleOptInFrom: z.string().nullable().optional(),
doubleOptInSubject: z.string().optional(),
doubleOptInContent: z.string().optional(),
}),
)
.mutation(async ({ ctx: { contactBook }, input }) => {
const { contactBookId, ...data } = input;
return contactBookService.updateContactBook(contactBook.id, data);
}),
deleteContactBook: contactBookProcedure
.input(z.object({ contactBookId: z.string() }))
.mutation(async ({ ctx: { contactBook }, input }) => {
return contactBookService.deleteContactBook(contactBook.id);
}),
deleteContactBook: contactBookProcedure
.input(z.object({ contactBookId: z.string() }))
.mutation(async ({ ctx: { contactBook }, input }) => {
return contactBookService.deleteContactBook(contactBook.id);
}),
contacts: contactBookProcedure
.input(
z.object({
page: z.number().optional(),
subscribed: z.boolean().optional(),
search: z.string().optional(),
}),
)
.query(async ({ ctx: { db }, input }) => {
const page = input.page || 1;
const limit = 30;
const offset = (page - 1) * limit;
contacts: contactBookProcedure
.input(
z.object({
page: z.number().optional(),
subscribed: z.boolean().optional(),
search: z.string().optional(),
}),
)
.query(async ({ ctx: { db }, input }) => {
const page = input.page || 1;
const limit = 30;
const offset = (page - 1) * limit;
const whereConditions: Prisma.ContactFindManyArgs["where"] = {
contactBookId: input.contactBookId,
...(input.subscribed !== undefined
? { subscribed: input.subscribed }
: {}),
...(input.search
? {
OR: [
{ email: { contains: input.search, mode: "insensitive" } },
{ firstName: { contains: input.search, mode: "insensitive" } },
{ lastName: { contains: input.search, mode: "insensitive" } },
],
}
: {}),
};
const whereConditions: Prisma.ContactFindManyArgs["where"] = {
contactBookId: input.contactBookId,
...(input.subscribed !== undefined
? { subscribed: input.subscribed }
: {}),
...(input.search
? {
OR: [
{ email: { contains: input.search, mode: "insensitive" } },
{ firstName: { contains: input.search, mode: "insensitive" } },
{ lastName: { contains: input.search, mode: "insensitive" } },
],
}
: {}),
};
const countP = db.contact.count({ where: whereConditions });
const countP = db.contact.count({ where: whereConditions });
const contactsP = db.contact.findMany({
where: whereConditions,
select: {
id: true,
email: true,
firstName: true,
lastName: true,
subscribed: true,
createdAt: true,
contactBookId: true,
unsubscribeReason: true,
},
orderBy: {
createdAt: "desc",
},
skip: offset,
take: limit,
});
const contactsP = db.contact.findMany({
where: whereConditions,
select: {
id: true,
email: true,
firstName: true,
lastName: true,
subscribed: true,
createdAt: true,
contactBookId: true,
unsubscribeReason: true,
},
orderBy: {
createdAt: "desc",
},
skip: offset,
take: limit,
});
const [contacts, count] = await Promise.all([contactsP, countP]);
const [contacts, count] = await Promise.all([contactsP, countP]);
return { contacts, totalPage: Math.ceil(count / limit) };
}),
return { contacts, totalPage: Math.ceil(count / limit) };
}),
addContacts: contactBookProcedure
.input(
z.object({
contacts: z
.array(
z.object({
email: z.string(),
firstName: z.string().optional(),
lastName: z.string().optional(),
properties: z.record(z.string()).optional(),
subscribed: z.boolean().optional(),
}),
)
.max(50000),
}),
)
.mutation(async ({ ctx: { contactBook, team }, input }) => {
return contactService.bulkAddContacts(
contactBook.id,
input.contacts,
team.id,
);
}),
addContacts: contactBookProcedure
.input(
z.object({
contacts: z
.array(
z.object({
email: z.string(),
firstName: z.string().optional(),
lastName: z.string().optional(),
properties: z.record(z.string()).optional(),
subscribed: z.boolean().optional(),
}),
)
.max(50000),
}),
)
.mutation(async ({ ctx: { contactBook, team }, input }) => {
return contactService.bulkAddContacts(
contactBook.id,
input.contacts,
team.id,
);
}),
updateContact: contactBookProcedure
.input(
z.object({
contactId: z.string(),
email: z.string().optional(),
firstName: z.string().optional(),
lastName: z.string().optional(),
properties: z.record(z.string()).optional(),
subscribed: z.boolean().optional(),
}),
)
.mutation(async ({ ctx: { contactBook, team }, input }) => {
const { contactId, ...contact } = input;
const updatedContact = await contactService.updateContactInContactBook(
contactId,
contactBook.id,
contact,
team.id,
);
updateContact: contactBookProcedure
.input(
z.object({
contactId: z.string(),
email: z.string().optional(),
firstName: z.string().optional(),
lastName: z.string().optional(),
properties: z.record(z.string()).optional(),
subscribed: z.boolean().optional(),
}),
)
.mutation(async ({ ctx: { contactBook, team }, input }) => {
const { contactId, ...contact } = input;
const updatedContact = await contactService.updateContactInContactBook(
contactId,
contactBook.id,
contact,
team.id,
);
if (!updatedContact) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Contact not found",
});
}
if (!updatedContact) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Contact not found",
});
}
return updatedContact;
}),
return updatedContact;
}),
deleteContact: contactBookProcedure
.input(z.object({ contactId: z.string() }))
.mutation(async ({ ctx: { contactBook, team }, input }) => {
const deletedContact = await contactService.deleteContactInContactBook(
input.contactId,
contactBook.id,
team.id,
);
deleteContact: contactBookProcedure
.input(z.object({ contactId: z.string() }))
.mutation(async ({ ctx: { contactBook, team }, input }) => {
const deletedContact = await contactService.deleteContactInContactBook(
input.contactId,
contactBook.id,
team.id,
);
if (!deletedContact) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Contact not found",
});
}
if (!deletedContact) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Contact not found",
});
}
return deletedContact;
}),
return deletedContact;
}),
bulkDeleteContacts: contactBookProcedure
.input(z.object({ contactIds: z.array(z.string()).min(1).max(1000) }))
.mutation(async ({ ctx: { contactBook, team }, input }) => {
const deletedContacts =
await contactService.bulkDeleteContactsInContactBook(
input.contactIds,
contactBook.id,
team.id,
);
return { count: deletedContacts.length };
}),
resendDoubleOptInConfirmation: contactBookProcedure
.input(z.object({ contactId: z.string() }))