Skip to content

Commit

Permalink
add support for translated validation messages (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo Vieira authored May 29, 2023
1 parent 3c81090 commit 091f9d1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
55 changes: 47 additions & 8 deletions packages/api/src/services/attestation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Contract } from '@ethersproject/contracts';
import { JsonRpcProvider } from '@ethersproject/providers';
import { parseEther } from '@ethersproject/units';
import { Wallet } from '@ethersproject/wallet';
import { database } from '@impactmarket/core';
import { database, utils } from '@impactmarket/core';
import { randomBytes, randomInt } from 'crypto';
import { Op } from 'sequelize';

Expand All @@ -20,6 +20,9 @@ import config from '../../config';
import { sendEmail } from '../../services/email';
import { sendSMS } from '../sms';

const { client: prismic } = utils.prismic;
const { locales } = utils;

interface IOdisPaymentsContract extends Contract {
payInCUSD(account: string, value: BigNumber): Promise<TransactionResponse>;
}
Expand Down Expand Up @@ -195,14 +198,33 @@ export const send = async (
) => {
let code = '';

const user = await database.models.appUser.findOne({
attributes: ['language'],
where: { id: userId },
});
const locale = locales.find(({ shortCode }) => user?.language.toLowerCase() === shortCode.toLowerCase())?.code;
if (type === AttestationType.PHONE_NUMBER) {
code = randomInt(1000, 9999).toString();

// TODO: add message per language
const body = 'Your verification code is: ' + code + '. - impactMarket';
const response = await prismic.getAllByType('push_notifications_data', {
lang: locale || 'en-US',
});
let smsPhoneValidationMessage: string | undefined;
if (response.length > 0) {
const data = response[0].data;
smsPhoneValidationMessage = data['sms-phone-validation-message'];
}
if (!smsPhoneValidationMessage) {
const response = await prismic.getAllByType('push_notifications_data', {
lang: 'en-US',
});
const data = response[0].data;
smsPhoneValidationMessage = data['sms-phone-validation-message'];
}
const body = smsPhoneValidationMessage!.replace('{{code}}', code);

sendSMS(plainTextIdentifier, body);

//
await database.models.appUser.update(
{
phone: plainTextIdentifier,
Expand All @@ -212,17 +234,34 @@ export const send = async (
} else if (type === AttestationType.EMAIL) {
code = randomBytes(4).toString('hex');

// TODO: add message per language
const body = 'Your verification code is: ' + code + '. - impactMarket';
const response = await prismic.getAllByType('push_notifications_data', {
lang: locale || 'en-US',
});
let emailValidationSubject: string | undefined;
let emailValidationBody: string | undefined;
if (response.length > 0) {
const data = response[0].data;
emailValidationSubject = data['email-validation-subject'];
emailValidationBody = data['email-validation-body'];
}
if (!emailValidationSubject || !emailValidationBody) {
const response = await prismic.getAllByType('push_notifications_data', {
lang: 'en-US',
});
const data = response[0].data;
emailValidationSubject = data['email-validation-subject'];
emailValidationBody = data['email-validation-body'];
}
const body = emailValidationBody!.replace('{{code}}', code);

sendEmail({
to: plainTextIdentifier,
// TODO: move to env
from: 'hello@impactmarket.com',
subject: 'impactMarket - Verification Code',
subject: emailValidationSubject,
text: body,
});

//
await database.models.appUser.update(
{
email: plainTextIdentifier,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as jwt from './jwt';
import { Logger } from './logger';
import * as util from './util';
import * as pushNotification from './pushNotification';
import * as prismic from './prismic';
import locales from './locale.json';

/**
* format object's values to number if they are numbers in string format
Expand All @@ -32,5 +34,7 @@ export {
util,
jwt,
cache,
pushNotification
pushNotification,
prismic,
locales
};
2 changes: 1 addition & 1 deletion packages/core/src/utils/prismic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as prismic from '@prismicio/client';

const endpoint = prismic.getEndpoint(process.env.PRISMIC_REPO!);
const endpoint = prismic.getRepositoryEndpoint(process.env.PRISMIC_REPO!);
const accessToken = process.env.PRISMIC_ACCESS_TOKEN;

export const client = prismic.createClient(endpoint, { accessToken });

0 comments on commit 091f9d1

Please sign in to comment.