Skip to content

Commit

Permalink
send email with contract (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo Vieira authored Jul 4, 2023
1 parent 4d83a2a commit 0a89298
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
23 changes: 6 additions & 17 deletions packages/core/src/services/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@ import sgMail, { MailDataRequired } from '@sendgrid/mail';

import config from '../config';

export default class Email {
constructor() {
if (config.sendgridApi.startsWith('SG.')) {
sgMail.setApiKey(config.sendgridApi);
}
export const sendEmail = (msg: MailDataRequired) => {
if (config.sendgridApi.startsWith('SG.')) {
sgMail.setApiKey(config.sendgridApi);
sgMail.send(msg).catch(console.error);
}

public async notify(msg: MailDataRequired) {
try {
await sgMail.send(msg);
} catch (error) {
console.error(error);

if (error.response) {
console.error(error.response.body);
}
}
}
}
return true;
};
2 changes: 0 additions & 2 deletions packages/core/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as app from './app';
import Email from './email';
import * as global from './global';
import { answer } from './learnAndEarn/answer';
import { registerClaimRewards } from './learnAndEarn/claimRewards';
Expand Down Expand Up @@ -34,7 +33,6 @@ export {
global,
storage,
ubi,
Email,
StoryServiceV2,
learnAndEarn,
MicroCredit,
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/services/microcredit/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { AppUserModel } from '../../database/models/app/appUser';
import { MicroCreditFormModel } from '../../database/models/microCredit/form';
import { BaseError } from '../../utils';
import { MicroCreditFormStatus } from '../../interfaces/microCredit/form';
import { sendEmail } from '../../services/email';
import { config } from '../../..';

export default class MicroCreditCreate {
private microCreditContentStorage = new MicroCreditContentStorage();
Expand All @@ -24,11 +26,41 @@ export default class MicroCreditCreate {
}
]
) {
const user = await models.appUser.findOne({
where: {
id: userId
}
});
if (!user) {
throw new BaseError('USER_NOT_FOUND', 'User not found');
}
const microCreditDocs = docs.map(doc => ({
...doc,
userId
}));

for (const doc of microCreditDocs) {
const { filepath, category } = doc;

if (category === 1 && user.email) {
const path = Buffer.from(
JSON.stringify({
bucket: config.aws.bucket.microCredit,
key: filepath
})
).toString('base64');
// send email to user
sendEmail({
to: user.email,
from: 'hello@impactmarket.com',
subject: `${
config.jsonRpcUrl.indexOf('alfajores') === -1 ? '' : '[TESTNET] '
}Your loan contract signed`,
text: `You can access your contract at ${config.imageHandlerUrl}/${path}`
});
}
}

await models.microCreditDocs.bulkCreate(microCreditDocs);

return microCreditDocs;
Expand Down

0 comments on commit 0a89298

Please sign in to comment.