feat: make billing better (#203)
This commit is contained in:
83
apps/web/src/server/service/contact-book-service.ts
Normal file
83
apps/web/src/server/service/contact-book-service.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { CampaignStatus, type ContactBook } from "@prisma/client";
|
||||
import { db } from "../db";
|
||||
import { LimitService } from "./limit-service";
|
||||
import { UnsendApiError } from "../public-api/api-error";
|
||||
|
||||
export async function getContactBooks(teamId: number, search?: string) {
|
||||
return db.contactBook.findMany({
|
||||
where: {
|
||||
teamId,
|
||||
...(search ? { name: { contains: search, mode: "insensitive" } } : {}),
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: { contacts: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function createContactBook(teamId: number, name: string) {
|
||||
const { isLimitReached, reason } =
|
||||
await LimitService.checkContactBookLimit(teamId);
|
||||
|
||||
if (isLimitReached) {
|
||||
throw new UnsendApiError({
|
||||
code: "FORBIDDEN",
|
||||
message: reason ?? "Contact book limit reached",
|
||||
});
|
||||
}
|
||||
|
||||
return db.contactBook.create({
|
||||
data: {
|
||||
name,
|
||||
teamId,
|
||||
properties: {},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getContactBookDetails(contactBookId: string) {
|
||||
const [totalContacts, unsubscribedContacts, campaigns] = await Promise.all([
|
||||
db.contact.count({
|
||||
where: { contactBookId },
|
||||
}),
|
||||
db.contact.count({
|
||||
where: { contactBookId, subscribed: false },
|
||||
}),
|
||||
db.campaign.findMany({
|
||||
where: {
|
||||
contactBookId,
|
||||
status: CampaignStatus.SENT,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: "desc",
|
||||
},
|
||||
take: 2,
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
totalContacts,
|
||||
unsubscribedContacts,
|
||||
campaigns,
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateContactBook(
|
||||
contactBookId: string,
|
||||
data: {
|
||||
name?: string;
|
||||
properties?: Record<string, string>;
|
||||
emoji?: string;
|
||||
}
|
||||
) {
|
||||
return db.contactBook.update({
|
||||
where: { id: contactBookId },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteContactBook(contactBookId: string) {
|
||||
return db.contactBook.delete({ where: { id: contactBookId } });
|
||||
}
|
Reference in New Issue
Block a user