@digitowl/backend-cores
v4.4.7
Published
==========================================
Downloads
326
Readme
DIGITOWL Backend Core
==========================================
A Typescript core backend in Typescript with services such as:
Language
This project uses Typescript for consistent and strict-typed code.
Project Structure
Initialize App
- The Project requires 2 config files needed in order to run.
- Please setup the all the project variables as listed on
/core/configs/index.ts. - Put the variable values on a
.envfile inside the root folder so the code can consume it when it runs.
DO NOT include
.envin the git repository under any circumstances.
- Run
npm startto prepare generated files from gql schemas and run the server.
Bind the dependencies
import { CoresDependencies } from "@digitowl/backend-cores";
const newContainer = new Container();
const registrationList = [
// ...Other dependencies
CoresDependencies,
];
for (const reg of registrationList) {
new reg().register(newContainer);
}
export const container = newContainer;Bind the dependency types
import { CoresDependencyTypes } from "@digitowl/backend-cores";
export const types = {
// ...Other dependency types
...CoresDependencyTypes
}Map errors
src/features/errors/failures/exceptionToFailure/index.ts
export class ExceptionToFailure {
public failureMap = new Map<string, Map<string, boolean>>([
[CoreFailureEnums.UPSTREAM_FAILURES, new Map<string, boolean>([
...CoreExceptionToFailure.failureMap.get(CoreFailureEnums.UPSTREAM_FAILURES),
])],
[CoreFailureEnums.DUPLICATE_OBJECT_EXIST, new Map<string, boolean>([
...CoreExceptionToFailure.failureMap.get(CoreFailureEnums.DUPLICATE_OBJECT_EXIST)
])],
[CoreFailureEnums.AUTHENTICATION_TOKEN_FAILED, new Map<string, boolean>([
...CoreExceptionToFailure.failureMap.get(CoreFailureEnums.AUTHENTICATION_TOKEN_FAILED),
])],
[CoreFailureEnums.OBJECT_NOT_FOUND, new Map<string, boolean>([
...CoreExceptionToFailure.failureMap.get(CoreFailureEnums.OBJECT_NOT_FOUND),
])]
]);
// ...Converter function
}$~~~~~~~~~~~$
Functions
SendGridMailServiceOperation
The SendGridMailServiceOperation class provides functionality for sending emails using SendGrid's email service. It supports sending templated emails with dynamic data fields.
Example
@inject(types.iSendGridMailServiceOperation)
private sendGridMailServiceOperation: iSendGridMailServiceOperation
const emailData = {
receiver: "[email protected]",
templateId: "d-1234567890abcdef1234567890abcdef",
actionUrl: "https://example.com/verify",
receiverName: "John Doe",
};
await this.sendGridMailServiceOperation.sendEmail(emailData)FirebaseMessagingOperation
The FirebaseMessagingOperation class provides functionality for sending multicast push notifications using Firebase Cloud Messaging (FCM). It interacts with the Firebase messaging client to handle message delivery to multiple devices.
Example
@inject(types.iFirebaseMessagingOperation)
private firebaseMessagingOperation: iFirebaseMessagingOperation
const message = {
tokens: ["token1", "token2"],
notification: {
title: "Sample Notification",
body: "This is a sample message.",
},
};
await this.firebaseMessagingOperation.sendMulticast(message, false)WhatsAppMessagingMessagesOperation
The WhatsAppMessagingMessagesOperation class provides functionality for sending messages via the WhatsApp Business API. It uses an injected client to manage configurations like the access token and business phone number ID, and it sends HTTP requests to the WhatsApp Graph API to dispatch messages.
Example
@inject(types.iWhatsAppMessagingMessagesOperation)
private whatsAppMessagingMessagesOperation: iWhatsAppMessagingMessagesOperation
const messageRequest = {
messaging_product: "whatsapp",
to: "recipient-phone-number",
type: "text",
text: {
body: "Hello, this is a message from the WhatsApp Business API!",
},
};
await this.whatsAppMessagingMessagesOperation.sendMessage(messageRequest)MidTransSnapPaymentGatewayOperation
The MidTransSnapPaymentGatewayOperation class provides functionality for interacting with the MidTrans Snap payment gateway. It supports creating payment transactions and verifying transaction notifications. This class integrates with a client that communicates with the MidTrans Snap API and handles upstream exceptions gracefully.
Example
@inject(types.iMidTransSnapPaymentGatewayOperation)
private midTransSnapPaymentGatewayOperation: iMidTransSnapPaymentGatewayOperation
// Example: Create a transaction
const transactionParameter = {
transaction_details: {
order_id: "ORDER-12345",
gross_amount: 100000,
},
credit_card: {
secure: true,
},
};
await this.midTransSnapPaymentGatewayOperation.createTransaction(transactionParameter)
// Example: Verify a notification
const notificationRequest = {
order_id: "ORDER-12345",
transaction_status: "settlement",
};
await this.midTransSnapPaymentGatewayOperation.verifyNotification(notificationRequest)BiteShipShipmentMapsAPIOperation
This class handles operations related to the BiteShip API for shipment maps. It includes methods to search for areas by keyword and to perform a detailed search of areas by their ID.
Example
@inject(types.iBiteShipShipmentMapsAPIOperation)
private biteShipShipmentMapsAPIOperation: iBiteShipShipmentMapsAPIOperation
const searchParams = {
input: "Jakarta",
type: "single"
};
await this.biteShipShipmentMapsAPIOperation.searchAreasByKeywordRequest(searchParam)
const detailParams = {
areaId: "some-area-id", // The ID of the area for which detailed information is needed
};
await this.biteShipShipmentMapsAPIOperation.doubleSearchAreasSecondRequest(detailParams)BiteShipShipmentRatesAPIOperation
The BiteShipShipmentRatesAPIOperation class is responsible for interacting with the BiteShip API to fetch shipment rates based on specified criteria such as origin and destination area IDs, selected couriers, and item details. It provides methods to retrieve rates based on the areas and couriers for a shipment, allowing you to calculate the costs associated with different delivery services.
Example
@inject(types.iBiteShipShipmentRatesAPIOperation)
private biteShipShipmentRatesAPIOperation: iBiteShipShipmentRatesAPIOperation
const rateRequest = {
origin_area_id: "origin-area-id", // The ID of the origin area
destination_area_id: "destination-area-id", // The ID of the destination area
couriers: "jne", // The couriers selected for shipment (comma-separated)
items: [
{
weight: 1.5, // Item weight in kg
length: 40, // Item length in cm
width: 30, // Item width in cm
height: 20, // Item height in cm
quantity: 1, // Number of items
value: 100 // Value of the item (in the currency of the area)
}
]
};
await this.biteShipShipmentRatesAPIOperation.getRatesByAreaId(rateRequest)BiteShipShipmentTrackingsAPIOperation
The BiteShipShipmentTrackingsAPIOperation class retrieves real-time tracking info for shipments using the waybill_id and courier_code from the BiteShip API.
Example
@inject(types.iBiteShipShipmentTrackingsAPIOperation)
private biteShipShipmentTrackingsAPIOperation: iBiteShipShipmentTrackingsAPIOperation
const trackingRequest = {
waybill_id: "your-waybill-id", // The unique identifier for the shipment
courier_code: "courier-code", // The code of the courier responsible for the shipment
};
await this.biteShipShipmentTrackingsAPIOperation.getTrackingsByWayBillID(trackingRequest)GoogleCloudStorageFileOperation
This class provides file operations (upload and delete) for Google Cloud Storage. It uses an injected Google Cloud Storage client to interact with the storage bucket.
Example
@inject(types.iGoogleCloudStorageFileOperation)
private googleCloudStorageFileOperation: iGoogleCloudStorageFileOperation
const buffer = fs.readFileSync("example.jpg");
const folder = "uploads";
const storageId = "user123";
const filenameWithExtension = "example.jpg";
// Upload file
const fileUrl = await this.googleCloudStorageFileOperation.uploadFile(
buffer,
folder,
storageId,
filenameWithExtension
);
// Delete file
await this.googleCloudStorageFileOperation.deleteFile(fileUrl);GoogleCloudStorageImageOperation
This class provides methods for interacting with Google Cloud Storage to handle image upload and deletion operations. It uses a dependency-injected client to interface with Google Cloud Storage.
Example
@inject(types.iGoogleCloudStorageImageOperation)
private googleCloudStorageImageOperation: iGoogleCloudStorageImageOperation
const imageBuffer = fs.readFileSync("example.jpg")
const category = "profile-pictures";
// Uplaod Image
const imageUrl = await this.googleCloudStorageImageOperation.uploadImage(imageBuffer, category)
// Delete image
await this.googleCloudStorageImageOperation.deleteImage(imageUrl)