letterxpress/ts/letterxpress.classes.account.ts

110 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-06-16 01:29:41 +00:00
import * as plugins from './letterxpress.plugins.js';
2019-11-16 16:54:45 +00:00
export interface ILetterXpressConstructorOptions {
2019-11-22 14:02:56 +00:00
username: string;
apiKey: string;
}
2019-11-16 16:54:45 +00:00
export class LetterXpressAccount {
2022-06-16 01:29:41 +00:00
// STATIC
2024-02-17 01:34:31 +00:00
public static async createAndStart(optionsArg: ConstructorParameters<typeof LetterXpressAccount>[0]) {
2022-06-16 01:29:41 +00:00
const letterXpressInstance = new LetterXpressAccount(optionsArg);
2024-02-17 01:34:31 +00:00
await letterXpressInstance.start();
2022-06-16 01:29:41 +00:00
return letterXpressInstance;
}
// INSTANCE
2024-02-16 19:27:14 +00:00
public baseApiUrl = 'https://api.letterxpress.de/v2';
public options: ILetterXpressConstructorOptions;
2024-02-16 19:27:14 +00:00
public deesDocumentInstance: plugins.deesDocument.PdfService;
2022-06-16 01:29:41 +00:00
public letterSentObservable = new plugins.smartrx.rxjs.Subject<plugins.tsclass.business.ILetter>();
2019-11-21 14:01:41 +00:00
constructor(optionsArg: ILetterXpressConstructorOptions) {
2022-06-16 01:29:41 +00:00
const done = plugins.smartpromise.defer();
this.options = optionsArg;
}
2024-02-17 01:34:31 +00:00
public async start() {
2024-02-16 19:27:14 +00:00
this.deesDocumentInstance = await plugins.deesDocument.PdfService.createAndStart({});
2022-06-16 01:29:41 +00:00
}
2019-11-21 14:01:41 +00:00
/**
* sends a letter
* @param letterArg
*/
2022-06-16 01:29:41 +00:00
public async sendLetter(letterArg: plugins.tsclass.business.ILetter) {
2024-02-16 19:27:14 +00:00
const pdfToSend = await this.deesDocumentInstance.createPdfFromLetterObject(letterArg);
const response = await this.request('/printjobs', 'POST', {
2019-11-22 14:02:56 +00:00
letter: {
2024-04-17 17:47:23 +00:00
base64_file: plugins.smartbuffer.uInt8ArrayToBase64(pdfToSend.buffer),
2024-02-16 19:27:14 +00:00
base64_file_checksum: await plugins.smarthash.md5FromString(
2024-04-17 17:47:23 +00:00
plugins.smartbuffer.uInt8ArrayToBase64(pdfToSend.buffer)
2019-11-22 14:02:56 +00:00
),
specification: {
color: '4',
mode: 'simplex',
2024-02-16 19:27:14 +00:00
shipping: letterArg.to.address.country === 'Germany' ? 'national' : 'international',
2022-06-16 01:29:41 +00:00
},
},
2019-11-22 14:02:56 +00:00
});
2022-06-16 01:29:41 +00:00
letterArg.objectActions.push({
name: 'letterxpress-sent',
message: 'requested letterxpress to send the letter',
privateMessage: null,
2024-02-16 19:27:14 +00:00
data: response.body.data.id,
2022-06-16 01:29:41 +00:00
timestamp: Date.now(),
userId: null
})
2019-11-22 14:02:56 +00:00
return letterArg;
}
2022-06-16 01:29:41 +00:00
public async cancelLetter(letterArg: plugins.tsclass.business.ILetter) {
const processingId = letterArg.objectActions.reduce<string>((previousVal, currentVal) => {
if (currentVal.name === 'letterxpress-sent') {
return currentVal.data
} else {
return previousVal;
}
}, null);
2019-11-22 14:02:56 +00:00
return await this.cancelLetterByProcessingId(processingId);
}
public async cancelLetterByProcessingId(processingId: string) {
2024-02-16 19:27:14 +00:00
const response = await this.request(`/printjobs/${processingId}`, 'DELETE', {});
2019-11-22 14:02:56 +00:00
return response;
2019-11-21 14:01:41 +00:00
}
/**
* fires the request
*/
2022-06-16 01:29:41 +00:00
private async request(routeArg: string, methodArg: 'GET' | 'POST' | 'DELETE', payload: any) {
2019-11-22 14:02:56 +00:00
const requestUrl = `${this.baseApiUrl}${routeArg}`;
console.log(requestUrl);
2019-11-21 14:01:41 +00:00
const requestData = {
auth: {
2024-02-16 19:27:14 +00:00
mode: "live",
2019-11-22 14:02:56 +00:00
username: this.options.username,
2022-06-16 01:29:41 +00:00
apikey: this.options.apiKey,
2019-11-21 14:01:41 +00:00
},
2022-06-16 01:29:41 +00:00
...payload,
2019-11-21 14:01:41 +00:00
};
2024-02-16 19:27:14 +00:00
// console.log(methodArg);
// console.log(requestData);
2019-11-22 14:02:56 +00:00
const response = await plugins.smartrequest.request(requestUrl, {
2019-11-21 14:01:41 +00:00
method: methodArg,
headers: {
2022-06-16 01:29:41 +00:00
'Content-Type': 'application/json',
2019-11-21 14:01:41 +00:00
},
2022-06-16 01:29:41 +00:00
requestBody: JSON.stringify(requestData),
2024-02-19 00:35:46 +00:00
keepAlive: false,
2019-11-21 14:01:41 +00:00
});
2019-11-22 14:02:56 +00:00
console.log(response.body);
return response;
2019-11-21 14:01:41 +00:00
}
2022-06-16 01:29:41 +00:00
public async stop() {
2024-02-16 19:27:14 +00:00
await this.deesDocumentInstance.stop();
2022-06-16 01:29:41 +00:00
}
}