Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] send email on loan application submit #768

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/interfaces/app/appNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export enum NotificationType {
LOAN_ADDED,
LEARN_AND_EARN_DO_NEW_LESSON,
LOAN_STATUS_CHANGED,
LOAN_APPLICATION_SUBMITTED,
NEW_LOAN_SUBMITTED
}

Expand Down
85 changes: 83 additions & 2 deletions packages/core/src/services/microcredit/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AppUserModel } from '../../database/models/app/appUser';
import { BaseError } from '../../utils';
import { BaseError, prismic as basePrimisc, locales } from '../../utils';
import { MailDataRequired } from '@sendgrid/mail';
import {
MicroCreditApplication,
MicroCreditApplicationCreation,
Expand All @@ -14,6 +15,8 @@ import { models } from '../../database';
import { sendEmail } from '../../services/email';
import { sendNotification } from '../../utils/pushNotification';

const { client: prismic } = basePrimisc;

export default class MicroCreditCreate {
private microCreditContentStorage = new MicroCreditContentStorage();

Expand Down Expand Up @@ -189,7 +192,85 @@ export default class MicroCreditCreate {
}
})
.then(user => user && sendNotification([user], NotificationType.NEW_LOAN_SUBMITTED, true, true));
// TODO: send email to user with form
// below we will send email to user with form
// get user email and language to get text for template and send email
const urlToApplicationForm = `https://app.impactmarket.com/microcredit/form/${userForm.id}`;
const user = await models.appUser.findOne({
attributes: ['email', 'language', 'walletPNT', 'appPNT'],
where: {
id: userForm.userId
}
});
if (!user) {
throw new BaseError('USER_NOT_FOUND', 'User not found');
}

// get text for email template on user language defaulting to english
const locale = locales.find(({ shortCode }) => user?.language.toLowerCase() === shortCode.toLowerCase())
?.code;
const response = await prismic.getAllByType('push_notifications_data', {
lang: locale || 'en-US'
});
let submittedFormEmailNotificationSubject: string | undefined;
let submittedFormEmailNotificationTitle: string | undefined;
let submittedFormEmailNotificationSubtitle: string | undefined;
let submittedFormEmailNotificationViewApplication: string | undefined;
let submittedFormEmailNotificationViewNextSteps: string | undefined;
if (response.length > 0) {
const data = response[0].data;
submittedFormEmailNotificationSubject = data['submitted-form-email-notification-subject'];
submittedFormEmailNotificationTitle = data['submitted-form-email-notification-title'];
submittedFormEmailNotificationSubtitle = data['submitted-form-email-notification-subtitle'];
submittedFormEmailNotificationViewApplication =
data['submitted-form-email-notification-view-application'];
submittedFormEmailNotificationViewNextSteps = data['submitted-form-email-notification-next-steps'];
}
if (
!submittedFormEmailNotificationSubject ||
!submittedFormEmailNotificationTitle ||
!submittedFormEmailNotificationSubtitle ||
!submittedFormEmailNotificationViewApplication ||
!submittedFormEmailNotificationViewNextSteps
) {
const response = await prismic.getAllByType('push_notifications_data', {
lang: 'en-US'
});
const data = response[0].data;
submittedFormEmailNotificationSubject = data['submitted-form-email-notification-subject'];
submittedFormEmailNotificationTitle = data['submitted-form-email-notification-title'];
submittedFormEmailNotificationSubtitle = data['submitted-form-email-notification-subtitle'];
submittedFormEmailNotificationViewApplication =
data['submitted-form-email-notification-view-application'];
submittedFormEmailNotificationViewNextSteps = data['submitted-form-email-notification-next-steps'];
}

// build the email structure and send
const dynamicTemplateData = {
submittedFormEmailNotificationTitle,
submittedFormEmailNotificationSubtitle,
urlToApplicationForm,
submittedFormEmailNotificationViewApplication,
submittedFormEmailNotificationViewNextSteps
};
const personalizations = [
{
to: [{ email: user.email }],
subject: submittedFormEmailNotificationSubject,
dynamicTemplateData
}
];
const sendgridData: MailDataRequired = {
from: {
name: 'impactMarket',
email: 'hello@impactmarket.com'
},
personalizations,
templateId: 'd-b257690897ff41028d7ad8cabe88f8cb'
};
sendEmail(sendgridData);
sendNotification([user.toJSON()], NotificationType.LOAN_APPLICATION_SUBMITTED, true, true, {
url: urlToApplicationForm
});
}

if (created) {
Expand Down
Loading