Skip to content

Commit

Permalink
fix profile
Browse files Browse the repository at this point in the history
  • Loading branch information
rappix committed Aug 6, 2024
1 parent ec6d77b commit e2b3171
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
59 changes: 27 additions & 32 deletions src/routes/ccMarketplace/kyc/kyc-approval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as crypto from 'crypto';
import express, { Request, Response } from 'express';
import { prisma } from '../../../services/prisma';
import {queryChain, submitExtrinsic} from '../../../utils/chain';
import {getAccessToken, getUserInformation, loginTemplate} from '../../../utils/fractal';
import {getAccessToken, getUserInformation, kycOnChain, loginTemplate} from '../../../utils/fractal';
import { authKYC } from '../../authentification/auth-middleware';
import logger from "@/utils/logger";

Expand Down Expand Up @@ -43,15 +43,31 @@ router.get('/kyc/callback', async (req: Request, res: Response) => {

const { access_token } = await getAccessToken(code as string);


// step 2: get user information from fractal api
const user = await getUserInformation(access_token);

const { isVerified, newLevel } = user?.verification_cases?.reduce(
(acc, vc) => {
if (vc.status === 'done' && vc.level.includes('wallet-substrate')) {
acc.isVerified = true;
if (vc.level.includes('plus')) {
acc.newLevel = 4;
} else if (!acc.newLevel && vc.level.includes('basic')) {
acc.newLevel = 1;
}
}
return acc;
},
{ isVerified: false, newLevel: 0 }
)

// step 3: save data to db
for(const wallet of user.wallets) {
logger.info('wallet')
logger.info(JSON.stringify(wallet))

if(wallet?.currency === 'substrate') {
logger.info(`Processing address ${wallet.address}`)

await prisma.kYC.upsert({
where: {
profileAddress: wallet.address,
Expand All @@ -70,17 +86,22 @@ router.get('/kyc/callback', async (req: Request, res: Response) => {
}
},
FractalId: user.uid,
status: VerificationStatus.PENDING,
status: isVerified ? VerificationStatus.VERIFIED : VerificationStatus.PENDING,
FirstName: user.person.full_name.split(' ').slice(0, -1).join(' '),
Country: user.person.residential_address_country
.split(' ')
.slice(-1)
.join(' '),
},
update: {

status: isVerified ? VerificationStatus.VERIFIED : VerificationStatus.PENDING
}
});

// KYC On chain
if(isVerified) {
await kycOnChain(wallet.address, newLevel)
}
}
}

Expand Down Expand Up @@ -156,34 +177,8 @@ router.post('/webhook/kyc-approval', async (req: Request, res: Response) => {
all_kyc.map(async(kyc) => {
logger.info(`Processing address ${kyc.profileAddress}`)

const existingData = await queryChain('kycPallet', 'members', [kyc.profileAddress])

const match = existingData?.data?.toString().match(/KYCLevel(\d+)/);
const existingLevel = match ? Number(match[1]) : 0;
const newLevel = (level === 'plus') ? 4 : 1

// skip in some cases
if(level === 'basic' && existingLevel >= 1) {
logger.info('User is already level1 KYC. Skipping address.')
return
}
if(existingLevel === 4) {
logger.info('User is already level4 KYC. Skipping address.')
return
}


const call = existingLevel >= 1 ? 'modifyMember' : 'addMember'

// save on blockchain
// no need to do this in db since this is done by blockchain event listener later
const response = await submitExtrinsic('kycPallet', call, [kyc.profileAddress, `KYCLevel${newLevel}`]);

if(response.success) {
logger.info(`Address successfully KYCed to level ${newLevel}.`)
} else {
logger.error(`Failed to process. ${response.error}`)
}
await kycOnChain(kyc.profileAddress, newLevel)
})

return res.status(200).json({ success: true });
Expand Down
38 changes: 38 additions & 0 deletions src/utils/fractal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import axios from 'axios';
import * as uritemplate from 'uri-template';
import {queryChain, submitExtrinsic} from "@/utils/chain";
import logger from "@/utils/logger";

interface FractalUser {
uid: string;
emails: { address: string }[];
institution: string;
verification_cases: {
level: string,
status: string
}[],
person: {
date_of_birth: string;
full_name: string;
Expand Down Expand Up @@ -79,4 +85,36 @@ export async function getAccessToken(code: string): Promise<FractalToken> {
if (res.status !== 200 || !res) throw new Error('Error getting access token');

return res.data;
}

export async function kycOnChain(address: string, newLevel: number) {
const existingData = await queryChain('kycPallet', 'members', [address])

const match = existingData?.data?.toString().match(/KYCLevel(\d+)/);
const existingLevel = match ? Number(match[1]) : 0;

// skip in some cases
if(newLevel < 1) {
logger.info('Not verified. Skipping address.')
return
}
if(newLevel === 1 && existingLevel >= 1) {
logger.info('User is already level1 KYC. Skipping address.')
return
}
if(existingLevel === 4) {
logger.info('User is already level4 KYC. Skipping address.')
return
}

const call = existingLevel >= 1 ? 'modifyMember' : 'addMember'

// no need to do this in db since this is done by blockchain event listener later
const response = await submitExtrinsic('kycPallet', call, [address, `KYCLevel${newLevel}`]);

if(response.success) {
logger.info(`Address ${address} successfully KYCed to level ${newLevel}.`)
} else {
logger.error(`Failed to KYC address ${address}. ${response.error}`)
}
}

0 comments on commit e2b3171

Please sign in to comment.