Add unsend campaign feature (#45)
* Add unsend email editor Add email editor Add more email editor Add renderer partial Add more marketing email features * Add more campaign feature * Add variables * Getting there * campaign is there mfs * Add migration
This commit is contained in:
27
apps/web/src/server/public-api/api-utils.ts
Normal file
27
apps/web/src/server/public-api/api-utils.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Context } from "hono";
|
||||
import { db } from "../db";
|
||||
import { UnsendApiError } from "./api-error";
|
||||
|
||||
export const getContactBook = async (c: Context, teamId: number) => {
|
||||
const contactBookId = c.req.param("contactBookId");
|
||||
|
||||
if (!contactBookId) {
|
||||
throw new UnsendApiError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "contactBookId is mandatory",
|
||||
});
|
||||
}
|
||||
|
||||
const contactBook = await db.contactBook.findUnique({
|
||||
where: { id: contactBookId, teamId },
|
||||
});
|
||||
|
||||
if (!contactBook) {
|
||||
throw new UnsendApiError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Contact book not found for this team",
|
||||
});
|
||||
}
|
||||
|
||||
return contactBook;
|
||||
};
|
65
apps/web/src/server/public-api/api/contacts/add-contact.ts
Normal file
65
apps/web/src/server/public-api/api/contacts/add-contact.ts
Normal 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: "post",
|
||||
path: "/v1/contactBooks/{contactBookId}/contacts",
|
||||
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().optional() }),
|
||||
},
|
||||
},
|
||||
description: "Retrieve the user",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
function addContact(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 addContact;
|
82
apps/web/src/server/public-api/api/contacts/get-contact.ts
Normal file
82
apps/web/src/server/public-api/api/contacts/get-contact.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { PublicAPIApp } from "~/server/public-api/hono";
|
||||
import { getTeamFromToken } from "~/server/public-api/auth";
|
||||
import { db } from "~/server/db";
|
||||
import { UnsendApiError } from "../../api-error";
|
||||
import { getContactBook } from "../../api-utils";
|
||||
|
||||
const route = createRoute({
|
||||
method: "get",
|
||||
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({
|
||||
id: z.string(),
|
||||
firstName: z.string().optional().nullable(),
|
||||
lastName: z.string().optional().nullable(),
|
||||
email: z.string(),
|
||||
subscribed: z.boolean().default(true),
|
||||
properties: z.record(z.string()),
|
||||
contactBookId: z.string(),
|
||||
createdAt: z.string(),
|
||||
updatedAt: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Retrieve the contact",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
function getContact(app: PublicAPIApp) {
|
||||
app.openapi(route, async (c) => {
|
||||
const team = await getTeamFromToken(c);
|
||||
|
||||
await getContactBook(c, team.id);
|
||||
|
||||
const contactId = c.req.param("contactId");
|
||||
|
||||
const contact = await db.contact.findUnique({
|
||||
where: {
|
||||
id: contactId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
throw new UnsendApiError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Contact not found",
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure properties is a Record<string, string>
|
||||
const sanitizedContact = {
|
||||
...contact,
|
||||
properties: contact.properties as Record<string, string>,
|
||||
};
|
||||
|
||||
return c.json(sanitizedContact);
|
||||
});
|
||||
}
|
||||
|
||||
export default getContact;
|
@@ -0,0 +1,66 @@
|
||||
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 { getContactBook } from "../../api-utils";
|
||||
|
||||
const route = createRoute({
|
||||
method: "patch",
|
||||
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",
|
||||
}),
|
||||
}),
|
||||
body: {
|
||||
required: true,
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({
|
||||
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().optional() }),
|
||||
},
|
||||
},
|
||||
description: "Retrieve the user",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
function updateContactInfo(app: PublicAPIApp) {
|
||||
app.openapi(route, async (c) => {
|
||||
const team = await getTeamFromToken(c);
|
||||
|
||||
await getContactBook(c, team.id);
|
||||
const contactId = c.req.param("contactId");
|
||||
|
||||
const contact = await updateContact(contactId, c.req.valid("json"));
|
||||
|
||||
return c.json({ contactId: contact.id });
|
||||
});
|
||||
}
|
||||
|
||||
export default updateContactInfo;
|
@@ -50,7 +50,7 @@ const route = createRoute({
|
||||
}),
|
||||
},
|
||||
},
|
||||
description: "Retrieve the user",
|
||||
description: "Retrieve the email",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
@@ -2,6 +2,9 @@ import { getApp } from "./hono";
|
||||
import getDomains from "./api/domains/get-domains";
|
||||
import sendEmail from "./api/emails/send-email";
|
||||
import getEmail from "./api/emails/get-email";
|
||||
import addContact from "./api/contacts/add-contact";
|
||||
import updateContactInfo from "./api/contacts/update-contact";
|
||||
import getContact from "./api/contacts/get-contact";
|
||||
|
||||
export const app = getApp();
|
||||
|
||||
@@ -12,4 +15,9 @@ getDomains(app);
|
||||
getEmail(app);
|
||||
sendEmail(app);
|
||||
|
||||
/**Contact related APIs */
|
||||
addContact(app);
|
||||
updateContactInfo(app);
|
||||
getContact(app);
|
||||
|
||||
export default app;
|
||||
|
Reference in New Issue
Block a user