get contacts from emailid,ids (#99)

This commit is contained in:
KM Koushik
2025-02-06 00:27:03 +11:00
committed by GitHub
parent d224be07a2
commit b9d9037ec7
5 changed files with 270 additions and 81 deletions

View File

@@ -0,0 +1,3 @@
---
openapi: get /v1/contactBooks/{contactBookId}/contacts/
---

View File

@@ -535,9 +535,178 @@
} }
} }
} }
},
"get": {
"parameters": [
{
"schema": {
"type": "string",
"example": "cuiwqdj74rygf74"
},
"required": true,
"name": "contactBookId",
"in": "path"
},
{
"schema": {
"type": "string"
},
"required": false,
"name": "emails",
"in": "query"
},
{
"schema": {
"type": "number"
},
"required": false,
"name": "page",
"in": "query"
},
{
"schema": {
"type": "number"
},
"required": false,
"name": "limit",
"in": "query"
},
{
"schema": {
"type": "string"
},
"required": false,
"name": "ids",
"in": "query"
}
],
"responses": {
"200": {
"description": "Retrieve multiple contacts",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"firstName": {
"type": "string",
"nullable": true
},
"lastName": {
"type": "string",
"nullable": true
},
"email": {
"type": "string"
},
"subscribed": {
"type": "boolean",
"default": true
},
"properties": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"contactBookId": {
"type": "string"
},
"createdAt": {
"type": "string"
},
"updatedAt": {
"type": "string"
}
},
"required": [
"id",
"email",
"properties",
"contactBookId",
"createdAt",
"updatedAt"
]
}
}
}
}
}
}
} }
}, },
"/v1/contactBooks/{contactBookId}/contacts/{contactId}": { "/v1/contactBooks/{contactBookId}/contacts/{contactId}": {
"patch": {
"parameters": [
{
"schema": {
"type": "string",
"example": "cuiwqdj74rygf74"
},
"required": true,
"name": "contactBookId",
"in": "path"
},
{
"schema": {
"type": "string",
"example": "cuiwqdj74rygf74"
},
"required": true,
"name": "contactId",
"in": "path"
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"properties": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"subscribed": {
"type": "boolean"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Retrieve the user",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"contactId": {
"type": "string"
}
}
}
}
}
}
}
},
"get": { "get": {
"parameters": [ "parameters": [
{ {
@@ -615,93 +784,17 @@
} }
} }
}, },
"patch": {
"parameters": [
{
"schema": {
"type": "string",
"example": "cuiwqdj74rygf74"
},
"required": true,
"name": "contactBookId",
"in": "path"
},
{
"schema": {
"type": "string",
"example": "cuiwqdj74rygf74"
},
"required": true,
"name": "contactId",
"in": "path"
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"properties": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"subscribed": {
"type": "boolean"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Retrieve the user",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"contactId": {
"type": "string"
}
}
}
}
}
}
}
},
"put": { "put": {
"summary": "Upsert a contact",
"description": "Create a new contact or update an existing one based on the contactId",
"parameters": [ "parameters": [
{ {
"schema": { "schema": {
"type": "string", "type": "string",
"minLength": 3,
"example": "cuiwqdj74rygf74" "example": "cuiwqdj74rygf74"
}, },
"required": true, "required": true,
"name": "contactBookId", "name": "contactBookId",
"in": "path" "in": "path"
},
{
"schema": {
"type": "string",
"example": "cuiwqdj74rygf74"
},
"required": true,
"name": "contactId",
"in": "path"
} }
], ],
"requestBody": { "requestBody": {
@@ -748,7 +841,10 @@
"contactId": { "contactId": {
"type": "string" "type": "string"
} }
} },
"required": [
"contactId"
]
} }
} }
} }
@@ -756,8 +852,6 @@
} }
}, },
"delete": { "delete": {
"summary": "Delete a contact",
"description": "Delete a contact from a contact book",
"parameters": [ "parameters": [
{ {
"schema": { "schema": {
@@ -789,7 +883,10 @@
"success": { "success": {
"type": "boolean" "type": "boolean"
} }
} },
"required": [
"success"
]
} }
} }
} }

View File

@@ -85,6 +85,7 @@
"group": "Contacts", "group": "Contacts",
"pages": [ "pages": [
"api-reference/contacts/get-contact", "api-reference/contacts/get-contact",
"api-reference/contacts/get-contacts",
"api-reference/contacts/create-contact", "api-reference/contacts/create-contact",
"api-reference/contacts/update-contact", "api-reference/contacts/update-contact",
"api-reference/contacts/upsert-contact", "api-reference/contacts/upsert-contact",

View 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",
request: {
params: z.object({
contactBookId: z.string().openapi({
param: {
name: "contactBookId",
in: "path",
},
example: "cuiwqdj74rygf74",
}),
}),
query: z.object({
emails: z.string().optional(),
page: z.number().optional(),
limit: z.number().optional(),
ids: z.string().optional(),
}),
},
responses: {
200: {
content: {
"application/json": {
schema: z.array(
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 multiple contacts",
},
},
});
function getContacts(app: PublicAPIApp) {
app.openapi(route, async (c) => {
const team = await getTeamFromToken(c);
await getContactBook(c, team.id);
const contactIds = c.req.query("ids")?.split(",");
const emails = c.req.query("emails")?.split(",");
const page = c.req.query("page") ? Number(c.req.query("page")) : 1;
const limit = c.req.query("limit") ? Number(c.req.query("limit")) : 5000;
const contacts = await db.contact.findMany({
where: {
id: { in: contactIds },
email: { in: emails },
},
skip: (page - 1) * limit,
take: limit,
});
// Ensure properties is a Record<string, string>
const sanitizedContacts = contacts.map((contact) => ({
...contact,
properties: contact.properties as Record<string, string>,
}));
return c.json(sanitizedContacts);
});
}
export default getContacts;

View File

@@ -7,6 +7,9 @@ import updateContactInfo from "./api/contacts/update-contact";
import getContact from "./api/contacts/get-contact"; import getContact from "./api/contacts/get-contact";
import updateEmailScheduledAt from "./api/emails/update-email"; import updateEmailScheduledAt from "./api/emails/update-email";
import cancelScheduledEmail from "./api/emails/cancel-email"; import cancelScheduledEmail from "./api/emails/cancel-email";
import getContacts from "./api/contacts/get-contacts";
import upsertContact from "./api/contacts/upsert-contact";
import deleteContact from "./api/contacts/delete-contact";
export const app = getApp(); export const app = getApp();
@@ -23,5 +26,8 @@ cancelScheduledEmail(app);
addContact(app); addContact(app);
updateContactInfo(app); updateContactInfo(app);
getContact(app); getContact(app);
getContacts(app);
upsertContact(app);
deleteContact(app);
export default app; export default app;