add upsert and delete to contacts (#67)
This commit is contained in:
3
apps/docs/api-reference/contacts/delete-contact.mdx
Normal file
3
apps/docs/api-reference/contacts/delete-contact.mdx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
openapi: delete /v1/contactBooks/{contactBookId}/contacts/{contactId}
|
||||||
|
---
|
3
apps/docs/api-reference/contacts/upsert-contact.mdx
Normal file
3
apps/docs/api-reference/contacts/upsert-contact.mdx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
openapi: put /v1/contactBooks/{contactBookId}/contacts/{contactId}
|
||||||
|
---
|
@@ -538,72 +538,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/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": [
|
||||||
{
|
{
|
||||||
@@ -680,6 +614,187 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"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": {
|
||||||
|
"summary": "Upsert a contact",
|
||||||
|
"description": "Create a new contact or update an existing one based on the contactId",
|
||||||
|
"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": {
|
||||||
|
"email": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"firstName": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"lastName": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"properties": {
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"subscribed": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"email"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Contact upserted successfully",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"contactId": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"summary": "Delete a contact",
|
||||||
|
"description": "Delete a contact from a contact book",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"schema": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "cuiwqdj74rygf74"
|
||||||
|
},
|
||||||
|
"required": true,
|
||||||
|
"name": "contactBookId",
|
||||||
|
"in": "path"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"schema": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "cuiwqdj74rygf74"
|
||||||
|
},
|
||||||
|
"required": true,
|
||||||
|
"name": "contactId",
|
||||||
|
"in": "path"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Contact deleted successfully",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"success": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -80,7 +80,9 @@
|
|||||||
"pages": [
|
"pages": [
|
||||||
"api-reference/contacts/get-contact",
|
"api-reference/contacts/get-contact",
|
||||||
"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/delete-contact"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@@ -0,0 +1,53 @@
|
|||||||
|
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 { getContactBook } from "../../api-utils";
|
||||||
|
|
||||||
|
const route = createRoute({
|
||||||
|
method: "delete",
|
||||||
|
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({ success: z.boolean() }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: "Contact deleted successfully",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
function deleteContactHandler(app: PublicAPIApp) {
|
||||||
|
app.openapi(route, async (c) => {
|
||||||
|
const team = await getTeamFromToken(c);
|
||||||
|
|
||||||
|
await getContactBook(c, team.id);
|
||||||
|
const contactId = c.req.param("contactId");
|
||||||
|
|
||||||
|
await deleteContact(contactId);
|
||||||
|
|
||||||
|
return c.json({ success: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default deleteContactHandler;
|
@@ -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: "put",
|
||||||
|
path: "/v1/contactBooks/{contactBookId}/contacts/{contactId}",
|
||||||
|
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() }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
description: "Contact upserted successfully",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
function upsertContact(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 upsertContact;
|
@@ -32,6 +32,22 @@ type UpdateContactResponse = {
|
|||||||
error: ErrorResponse | null;
|
error: ErrorResponse | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type UpsertContactPayload =
|
||||||
|
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["put"]["requestBody"]["content"]["application/json"];
|
||||||
|
|
||||||
|
type UpsertContactResponseSuccess =
|
||||||
|
paths["/v1/contactBooks/{contactBookId}/contacts/{contactId}"]["put"]["responses"]["200"]["content"]["application/json"];
|
||||||
|
|
||||||
|
type UpsertContactResponse = {
|
||||||
|
data: UpsertContactResponseSuccess | null;
|
||||||
|
error: ErrorResponse | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
type DeleteContactResponse = {
|
||||||
|
data: { success: boolean } | null;
|
||||||
|
error: ErrorResponse | null;
|
||||||
|
};
|
||||||
|
|
||||||
export class Contacts {
|
export class Contacts {
|
||||||
constructor(private readonly unsend: Unsend) {
|
constructor(private readonly unsend: Unsend) {
|
||||||
this.unsend = unsend;
|
this.unsend = unsend;
|
||||||
@@ -71,4 +87,28 @@ export class Contacts {
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async upsert(
|
||||||
|
contactBookId: string,
|
||||||
|
contactId: string,
|
||||||
|
payload: UpsertContactPayload
|
||||||
|
): Promise<UpsertContactResponse> {
|
||||||
|
const data = await this.unsend.put<UpsertContactResponseSuccess>(
|
||||||
|
`/contactBooks/${contactBookId}/contacts/${contactId}`,
|
||||||
|
payload
|
||||||
|
);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(
|
||||||
|
contactBookId: string,
|
||||||
|
contactId: string
|
||||||
|
): Promise<DeleteContactResponse> {
|
||||||
|
const data = await this.unsend.delete<{ success: boolean }>(
|
||||||
|
`/contactBooks/${contactBookId}/contacts/${contactId}`
|
||||||
|
);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
50
packages/sdk/types/schema.d.ts
vendored
50
packages/sdk/types/schema.d.ts
vendored
@@ -192,6 +192,7 @@ export interface paths {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
"/v1/contactBooks/{contactBookId}/contacts/{contactId}": {
|
"/v1/contactBooks/{contactBookId}/contacts/{contactId}": {
|
||||||
get: {
|
get: {
|
||||||
@@ -253,6 +254,55 @@ export interface paths {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
put: {
|
||||||
|
parameters: {
|
||||||
|
path: {
|
||||||
|
contactBookId: string;
|
||||||
|
contactId: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
requestBody: {
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
email: string;
|
||||||
|
firstName?: string;
|
||||||
|
lastName?: string;
|
||||||
|
properties?: {
|
||||||
|
[key: string]: string;
|
||||||
|
};
|
||||||
|
subscribed?: boolean;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
responses: {
|
||||||
|
/** @description Contact upserted successfully */
|
||||||
|
200: {
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
contactId: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
delete: {
|
||||||
|
parameters: {
|
||||||
|
path: {
|
||||||
|
contactBookId: string;
|
||||||
|
contactId: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
responses: {
|
||||||
|
/** @description Contact deleted successfully */
|
||||||
|
200: {
|
||||||
|
content: {
|
||||||
|
"application/json": {
|
||||||
|
success: boolean;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user