Skip to content

Commit

Permalink
Remove odis (#1132)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernardo <19441097+obernardovieira@users.noreply.github.com>
Co-authored-by: AnotherDev <anotherdev.eth@gmail.com>
  • Loading branch information
3 people authored Aug 26, 2024
1 parent d490069 commit d55368a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
13 changes: 11 additions & 2 deletions packages/api/src/controllers/v2/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import { getAddress } from '@ethersproject/address';

import { AppUserModel } from '@impactmarket/core/src/database/models/app/appUser';
import { AttestationType, send, verify } from '~services/attestation';
import { ListUserNotificationsRequestSchema } from '~validators/user';
import { RequestWithUser } from '~middlewares/core';
Expand All @@ -9,6 +10,13 @@ import { standardResponse } from '~utils/api';
import UserLogService from '~services/app/user/log';
import UserService from '~services/app/user';

type VerifyRequestBody = {
email: string;
code: string;
userId: number;
params?: Partial<AppUserModel>;
};

class UserController {
private userService: UserService;
private userLogService: UserLogService;
Expand Down Expand Up @@ -354,9 +362,10 @@ class UserController {
.catch(e => standardResponse(res, 400, false, '', { error: e.message }));
};
public verify = (req: RequestWithUser, res: Response) => {
const { email, code, userId } = req.body;
const { email, code, userId, params } = req.body as VerifyRequestBody;
const userParams: Partial<AppUserModel> = params ?? {};

verify(email, AttestationType.EMAIL_LINK, code!, userId)
verify(email, AttestationType.EMAIL_LINK, code!, userId, userParams)
.then(r => standardResponse(res, 200, true, r, {}))
.catch(e => standardResponse(res, 400, false, '', { error: e.message }));
};
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/routes/v2/protocol/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from 'express';

import microcredit from './microcredit';
import circulatingSupply from './circulatingSupply';
import microcredit from './microcredit';

export default (app: Router): void => {
const route = Router();
Expand Down
6 changes: 5 additions & 1 deletion packages/api/src/services/app/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ export default class UserService {
this._userRoles(userParams.address, userParams.clientId),
this._userRules(userParams.address, userParams.clientId)
]);
console.log('After promises', user, userRoles, userRules);
notificationsCount = await models.appNotification.count({
where: {
userId: user.id,
read: false
}
});
console.log('Notifications count', notificationsCount);
}

// we could prevent this update, but we don't want to make the user wait
Expand All @@ -168,9 +170,10 @@ export default class UserService {
user.update({ clientId: 2 });
}
}

console.log('Before last login');
this._updateLastLogin(user.id);

console.log('Before token');
const token = utils.jwt.generateAccessToken({
clientId: userParams.clientId,
address: userParams.address,
Expand Down Expand Up @@ -631,6 +634,7 @@ export default class UserService {
loanManager: null
};
}

const [userRoles, user] = await Promise.all([
getUserRoles(address),
models.appUser.findOne({
Expand Down
19 changes: 18 additions & 1 deletion packages/api/src/services/attestation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { database, utils } from '@impactmarket/core';
import { parseEther } from '@ethersproject/units';
import { randomBytes, randomInt } from 'crypto';

import { AppUserModel } from '@impactmarket/core/src/database/models/app/appUser';
import { sendEmail } from '../../services/email';
import { sendSMS } from '../sms';
import config from '../../config';
Expand Down Expand Up @@ -81,7 +82,13 @@ const topUpOdis = async (issuer: Wallet) => {
* @param userId user id doing the verification
* @returns the obfuscated identifier
*/
export const verify = async (plainTextIdentifier: string, type: AttestationType, code: string, userId: number) => {
export const verify = async (
plainTextIdentifier: string,
type: AttestationType,
code: string,
userId: number,
params?: Partial<AppUserModel>
) => {
// check if code exists and is valid
// TODO: create startup process to delete expired codes
const { gt } = Op;
Expand All @@ -98,6 +105,16 @@ export const verify = async (plainTextIdentifier: string, type: AttestationType,
if (!validCode) {
throw Error('Invalid or expired code');
}
if (type === AttestationType.EMAIL_LINK) {
await database.models.appUser.update(
{
emailValidated: true,
...params
},
{ where: { id: userId } }
);
return '';
}

// initiate odis request
const issuer = new Wallet(config.attestations.issuerPrivateKey, new JsonRpcProvider(config.chain.jsonRPCUrlCelo));
Expand Down
13 changes: 12 additions & 1 deletion packages/api/src/validators/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import config from '~config/index';

const validator = createValidator();

const appUserModelSchema = Joi.object({
firstName: Joi.string().optional(),
lastName: Joi.string().optional(),
age: Joi.number().optional(),
gender: Joi.string().optional(),
language: Joi.string().optional(),
currency: Joi.string().optional(),
country: Joi.string().optional()
}).optional();

const create = celebrate({
body: defaultSchema.object({
address: Joi.string().required(),
Expand Down Expand Up @@ -51,7 +61,8 @@ const verify = celebrate({
body: defaultSchema.object({
email: Joi.string().required(),
code: Joi.string().required(),
userId: Joi.string().required()
userId: Joi.string().required(),
params: appUserModelSchema
})
});

Expand Down

0 comments on commit d55368a

Please sign in to comment.