Skip to content

Commit

Permalink
feat(core): ✨ communication history store in database
Browse files Browse the repository at this point in the history
communication history store in database

Ref: #191
  • Loading branch information
PritamIT2023 committed Oct 26, 2024
1 parent fc7dca8 commit b30ece4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
88 changes: 83 additions & 5 deletions package/communication/communicate.communicator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { v4 as uuidv4 } from "uuid";
import { constant } from "../constants/server.constant";
import { databaseActions } from "../database/actions.database";
import { coreConstant } from "../index";
import { WrappidLogger } from "../logging/wrappid.logger";
import * as communicationUtils from "../utils/communication.utils";
import { checkIfCommunicationEnabled } from "../utils/communication.utils";
Expand Down Expand Up @@ -53,27 +55,61 @@ export const communicate = async ({
});
//create
// FunctionsRegistry.createCommunicationHistory();
const defaultProvider = await communicationUtils.getDefaultCommunicationConfig(commType);
const senderData = await mapCommunicationConfig(defaultProvider, commType);
// with status new give entry
const newCreatedData = await databaseActions.create(
"application",
"CommunicationHistories",
{
type: commType,
from: senderData?.sender,
to: commRecipients.to[0],
retryCount: 0,
commId: uuidv4(),
extraInfo: {},
variable: {otp: commData?.otp},
_status: "new",
templateId: communicationTemplate?.id,
}
);
let resData:any = {status: false, data: {}};

Check warning on line 76 in package/communication/communicate.communicator.ts

View workflow job for this annotation

GitHub Actions / Validate code style

Unexpected any. Specify a different type
if (directFlag) {
switch (commType) {
case constant.commType.EMAIL:
return communicateEmail({ ...commRecipients, ...messageObject });
resData = await communicateEmail({ ...commRecipients, ...messageObject });
break;
case constant.commType.SMS:
return communicateSMS({
resData = await communicateSMS({
phone: commRecipients.to[0],
...messageObject,
dlttemplateid: communicationTemplate.externalTemplateId,
});
break;
case constant.commType.WHATSAPP:
return communicateWhatsApp({
resData = await communicateWhatsApp({
phone: commRecipients.to[0],
messageObject,
});
break;
default:
WrappidLogger.error("Communication type is invalid.");
throw new Error("Communication type is invalid.");
}
//check communcation status
///update
//check communcation status
///update status sent
if(resData?.status){
databaseActions.update(
"application",
"CommunicationHistories",
{ _status: "sent",
extraInfo: {response: resData?.data}
},
{ where: { id: newCreatedData?.id } }
);
return {success:true};
}

} else {
// db entry
}
Expand All @@ -82,6 +118,15 @@ export const communicate = async ({
throw new Error("Communcation Disabled!!");
}
} catch (error:any) {

Check warning on line 120 in package/communication/communicate.communicator.ts

View workflow job for this annotation

GitHub Actions / Validate code style

Unexpected any. Specify a different type
//db entry with status sent faild
databaseActions.update(
"application",
"CommunicationHistories",
{ _status: "faild" },
{ where: { to: commRecipients[0],
variable: {otp: commData?.otp},
_status: "new", } }
);
// console.error(error);
WrappidLogger.error(error);
if (errorFlag) {
Expand All @@ -91,3 +136,36 @@ export const communicate = async ({
WrappidLogger.logFunctionEnd("whatsapp.communicate");
}
};

/**
* This function help us to map communication config
* @param configData config data
* @param commType communication type
* @returns
*/
async function mapCommunicationConfig(configData:any, commType:string){

Check warning on line 146 in package/communication/communicate.communicator.ts

View workflow job for this annotation

GitHub Actions / Validate code style

Unexpected any. Specify a different type
try {
WrappidLogger.logFunctionStart("mapCommunicationConfig");
const returndata:any = {sender: ""};

Check warning on line 149 in package/communication/communicate.communicator.ts

View workflow job for this annotation

GitHub Actions / Validate code style

Unexpected any. Specify a different type
switch (commType) {
case coreConstant.commType.EMAIL:
returndata.sender = configData?.fromEmail;
break;

case coreConstant.commType.SMS:
returndata.sender = configData?.sender;
break;

case coreConstant.commType.WHATSAPP:
returndata.sender = configData?.api_url;
break;

default:
break;
}
return returndata;
} catch (error:any) {

Check warning on line 167 in package/communication/communicate.communicator.ts

View workflow job for this annotation

GitHub Actions / Validate code style

Unexpected any. Specify a different type
WrappidLogger.error(error);
throw error;
}
}
3 changes: 2 additions & 1 deletion package/communication/email/email.communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const communicate = async (mailOptions: any) => {
WrappidLogger.info("Email sent.................");
WrappidLogger.info(<string>res);
// console.log(res);
return true;
return {status: true, data:res};
// return true;
})
.catch((error) => {
WrappidLogger.info("Email sent failed.................");
Expand Down
3 changes: 2 additions & 1 deletion package/communication/sms/sms.communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const communicate = async (smsOptions: any) => {
.then((data: any) => {
console.log(data);
if (data.includes("Sent.c")) {
return true;
return { status: true, data: data };
// return true;
} else {
WrappidLogger.error("SMS sent failed");
throw new Error("SMS sent failed");
Expand Down
4 changes: 1 addition & 3 deletions package/communication/whatsapp/whatsapp.communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ async function communicate(whatsappOptions: any) {
throw data.error;
} else
return {
status: 200,
success: true,
error: null,
status: true,
data: data,
};
})
Expand Down

0 comments on commit b30ece4

Please sign in to comment.