From 7d9b8a94981730649e7c504615fe2895aff42437 Mon Sep 17 00:00:00 2001 From: Jonathan Derrough Date: Wed, 11 Dec 2024 20:23:45 +0100 Subject: [PATCH] feat: Adds saveInvoice --- src/client.test.ts | 103 +++++++++++++++++++++++++++++++++++++++++++++ src/client.ts | 15 +++++++ src/tests/env.ts | 5 +++ 3 files changed, 123 insertions(+) diff --git a/src/client.test.ts b/src/client.test.ts index 00ac399..d110f3e 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -47,6 +47,109 @@ test("Get next invoice number", async () => { assert.type(nextInvoiceNumber, "string"); }); +test("Create a new invoice", async () => { + const { objects: invoiceNumber } = await sevDeskClient.getNextInvoiceNumber({ + invoiceType: "RE", + useNextNumber: true, + }); + const { objects: contacts } = await sevDeskClient.getContacts(); + + const { + objects: { invoice }, + } = await sevDeskClient.saveInvoice({ + invoice: { + // id: null, + objectName: "Invoice", + invoiceNumber, + contact: { + id: contacts[0].id, + objectName: "Contact", + }, + contactPerson: { + id: env.CONTACT_PERSON_ID, + objectName: "SevUser", + }, + invoiceDate: "01.01.2022", + header: `Invoice ${invoiceNumber}`, + headText: "header information", + footText: "footer information", + timeToPay: 20, + discount: 0, + address: "name\nstreet\npostCode city", + addressCountry: { + id: 1, + objectName: "StaticCountry", + }, + payDate: "2019-08-24T14:15:22Z", + deliveryDate: "01.01.2022", + deliveryDateUntil: null, + status: "100", + smallSettlement: 0, + taxRate: 0, + taxRule: { + id: "1", + objectName: "TaxRule", + }, + taxText: "Umsatzsteuer 19%", + taxType: "default", + taxSet: null, + paymentMethod: { + id: 21919, + objectName: "PaymentMethod", + }, + sendDate: "01.01.2020", + invoiceType: "RE", + currency: "EUR", + showNet: "1", + sendType: "VPR", + origin: null, + customerInternalNote: null, + propertyIsEInvoice: false, + mapAll: true, + }, + invoicePosSave: [ + { + id: null, + objectName: "InvoicePos", + mapAll: true, + // part: { + // id: 0, + // objectName: 'Part', + // }, + quantity: 1, + price: 100, + name: "Dragonglass", + unity: { + id: 1, + objectName: "Unity", + }, + positionNumber: 0, + text: "string", + discount: 0.1, + taxRate: 19, + priceGross: 100, + priceTax: 0.1, + }, + ], + invoicePosDelete: null, + filename: "string", + discountSave: [ + { + discount: "true", + text: "string", + percentage: true, + value: 0, + objectName: "Discounts", + mapAll: "true", + }, + ], + discountDelete: null, + }); + + console.log(invoice); + assertIsInvoice(invoice); +}); + test("Get document folders", async () => { const { objects: documentFolders } = await sevDeskClient.getDocumentFolders(); diff --git a/src/client.ts b/src/client.ts index c660415..6ebd50e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -113,6 +113,21 @@ export class SevDeskClient { }>(url, { method: "GET" }); } + /** + * Create a new invoice + */ + async saveInvoice(body: unknown) { + const url = this.urls.apiSaveInvoiceUrl(); + + return this.request<{ + objects: { invoice: Required }; + }>(url, { + method: "POST", + body: JSON.stringify(body), + headers: { "Content-Type": "application/json" }, + }); + } + // ------------------------------------------------------- // DocumentFolder // ------------------------------------------------------- diff --git a/src/tests/env.ts b/src/tests/env.ts index b85e50b..b335f7b 100644 --- a/src/tests/env.ts +++ b/src/tests/env.ts @@ -4,3 +4,8 @@ export const TEST_SEVDESK_API_TOKEN = env .get("TEST_SEVDESK_API_TOKEN") .required() .asString(); + +export const CONTACT_PERSON_ID = env + .get("CONTACT_PERSON_ID") + .required() + .asString();