From f951986f2a8b1431198e50ed107a7fe3914359b4 Mon Sep 17 00:00:00 2001 From: Esraa Jbara Date: Tue, 4 Jan 2022 15:06:30 +0200 Subject: [PATCH 1/3] add get all users to service --- api/src/database/service/User.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/api/src/database/service/User.ts b/api/src/database/service/User.ts index 848527e4..49432170 100644 --- a/api/src/database/service/User.ts +++ b/api/src/database/service/User.ts @@ -6,6 +6,7 @@ import { IBusinessDwollaId, ICustomerDwollaId, INewUserInput, + IDBUser, } from "src/types"; import { User, User as UserSchema } from "../schema"; import { removeMongoMeta } from "../utils/index"; @@ -37,14 +38,12 @@ export async function get(filter: any): Promise { return removeMongoMeta(response?.toObject()); } +export async function getAll(): Promise { + const response = await User.find(); + return response?.length > 0 ? response.map((doc) => removeMongoMeta(doc.toObject())) : []; +} + export async function getBusinesses(): Promise { const response = await User.find({ verifiedBusiness: true }); - if (response?.length > 0) { - const result: Business[] = []; - response.forEach((element) => { - const business = removeMongoMeta(element.toObject()).business; - result.push(business); - }); - return result; - } else return []; -} + return response?.length > 0 ? response.map((doc) => removeMongoMeta(doc.toObject()).business) : []; +}; From a8bd576adb7fa4e338edd12bd8d512bde4297d10 Mon Sep 17 00:00:00 2001 From: Esraa Jbara Date: Tue, 4 Jan 2022 15:06:57 +0200 Subject: [PATCH 2/3] add users statas --- api/src/router/stats/controller.ts | 59 ++++++++++++++++++++++++++++ api/src/router/stats/index.ts | 1 + api/src/test/Operator.server.test.ts | 14 +++++++ api/src/types/index.ts | 13 ++++++ 4 files changed, 87 insertions(+) diff --git a/api/src/router/stats/controller.ts b/api/src/router/stats/controller.ts index 4a89e348..6d9be9b6 100644 --- a/api/src/router/stats/controller.ts +++ b/api/src/router/stats/controller.ts @@ -1,4 +1,5 @@ import { Request, Response } from "express"; +import * as PublicServices from "src/service/PublicService"; import { getFundingStatus, getDeposits, @@ -7,11 +8,15 @@ import { } from "src/service/PublicService"; import { httpUtils } from "src/utils"; import { + IDBUser, + StatsUser, IDeposit, IWithdrawal, IOperatorTotal, ITransferEvent, + IWallet } from "src/types"; +import { UserService as UserDatabaseService } from "src/database/service"; const codes = httpUtils.codes; @@ -62,3 +67,57 @@ export async function getAllTransfers( httpUtils.serverError(err, res); } } + +export async function getUsersStats( + req: Request, + res: Response +): Promise { + try { + const users: IDBUser[] = await UserDatabaseService.getAll(); + if(!users?.length) return []; + const businesses: StatsUser[] = []; + const customers: StatsUser[] = []; + + await Promise.all(users.map(async (u: IDBUser): Promise => { + if(u.verifiedBusiness) { + const business = u.business; + const walletData: IWallet = await PublicServices.getWallet(business.dwollaId) + const user: StatsUser = { + firstName: business.owner.firstName, + lastName: business.owner.lastName, + email: u.email, + dwollaId: business.dwollaId, + balance: walletData.availableBalance, + lastLogin: 0, + walletAddress: business.walletAddress, + address: `${business.address1} ${business.address2}`, + type: 'business' + } + businesses.push(user); + } + if(u.verifiedCustomer) { + const customer = u.customer; + const walletData: IWallet = await PublicServices.getWallet(customer.dwollaId) + const user: StatsUser = { + firstName: customer.firstName, + lastName: customer.lastName, + email: u.email, + dwollaId: customer.dwollaId, + balance: walletData.availableBalance, + lastLogin: 0, + walletAddress: customer.walletAddress, + address: `${customer.address1} ${customer.address2}`, + type: 'customer' + } + customers.push(user); + } + return; + })) + + const all = customers.concat(businesses); + httpUtils.createHttpResponse(all, codes.OK, res); + } catch(err) { + httpUtils.serverError(err, res); + } +}; + diff --git a/api/src/router/stats/index.ts b/api/src/router/stats/index.ts index ff00211f..d7994319 100644 --- a/api/src/router/stats/index.ts +++ b/api/src/router/stats/index.ts @@ -7,5 +7,6 @@ stats.get("/stats/deposit", controller.getAllDeposits); stats.get("/stats/withdrawal", controller.getAllWithdrawals); stats.get("/stats/operator", controller.getOperatorStatistics); stats.get("/stats/transfer", controller.getAllTransfers); +stats.get("/stats/users", controller.getUsersStats); export default stats; diff --git a/api/src/test/Operator.server.test.ts b/api/src/test/Operator.server.test.ts index 3e2fab86..de49eefb 100644 --- a/api/src/test/Operator.server.test.ts +++ b/api/src/test/Operator.server.test.ts @@ -1327,6 +1327,20 @@ describe("Operator endpoints test", () => { done(err); }); }); + + it("GET /stats/users: it should retrieve all users, HTTP 200", (done) => { + chai + .request(server) + .get("/stats/users") + .then((res) => { + expect(res).to.have.status(codes.OK); + log(JSON.parse(res.text)); + done(); + }) + .catch((err) => { + done(err); + }); + }); }); describe("GET /users/:id/notifications", () => { diff --git a/api/src/types/index.ts b/api/src/types/index.ts index 88d65c4d..8617d357 100644 --- a/api/src/types/index.ts +++ b/api/src/types/index.ts @@ -216,3 +216,16 @@ export type GenericDatabaseResponse = { data?: T; error?: E; }; + +export interface StatsUser { + firstName: string + lastName: string + email: string + dwollaId: string + balance: number + lastLogin: number + walletAddress: string + address: string + type: 'customer' | 'business' +} + From 074414335143f35a8f62af37feda234ac1ddf55e Mon Sep 17 00:00:00 2001 From: Esraa Jbara Date: Tue, 4 Jan 2022 15:11:01 +0200 Subject: [PATCH 3/3] prettier --- api/src/database/service/User.ts | 10 ++- api/src/middlewares/index.ts | 7 +- api/src/router/stats/controller.ts | 107 +++++++++++++++-------------- api/src/types/index.ts | 19 +++-- 4 files changed, 75 insertions(+), 68 deletions(-) diff --git a/api/src/database/service/User.ts b/api/src/database/service/User.ts index 49432170..604cdf66 100644 --- a/api/src/database/service/User.ts +++ b/api/src/database/service/User.ts @@ -40,10 +40,14 @@ export async function get(filter: any): Promise { export async function getAll(): Promise { const response = await User.find(); - return response?.length > 0 ? response.map((doc) => removeMongoMeta(doc.toObject())) : []; + return response?.length > 0 + ? response.map((doc) => removeMongoMeta(doc.toObject())) + : []; } export async function getBusinesses(): Promise { const response = await User.find({ verifiedBusiness: true }); - return response?.length > 0 ? response.map((doc) => removeMongoMeta(doc.toObject()).business) : []; -}; + return response?.length > 0 + ? response.map((doc) => removeMongoMeta(doc.toObject()).business) + : []; +} diff --git a/api/src/middlewares/index.ts b/api/src/middlewares/index.ts index b42037a4..de0f17f5 100644 --- a/api/src/middlewares/index.ts +++ b/api/src/middlewares/index.ts @@ -8,10 +8,9 @@ export const verifyRequest: express.RequestHandler = async ( response: express.Response, next: express.NextFunction ) => { - if(process.env.NODE_ENV=="test"){ + if (process.env.NODE_ENV == "test") { next(); - } - else{ + } else { const authHeader = request?.headers?.authorization; if (!authHeader) { response @@ -39,7 +38,7 @@ export const verifyRequest: express.RequestHandler = async ( .send({ message: "Internal error while verifying request!" }); } } - } + } }; export const mwVaildator = ( diff --git a/api/src/router/stats/controller.ts b/api/src/router/stats/controller.ts index 6d9be9b6..71790d5f 100644 --- a/api/src/router/stats/controller.ts +++ b/api/src/router/stats/controller.ts @@ -8,13 +8,13 @@ import { } from "src/service/PublicService"; import { httpUtils } from "src/utils"; import { - IDBUser, - StatsUser, + IDBUser, + StatsUser, IDeposit, IWithdrawal, IOperatorTotal, ITransferEvent, - IWallet + IWallet, } from "src/types"; import { UserService as UserDatabaseService } from "src/database/service"; @@ -71,53 +71,58 @@ export async function getAllTransfers( export async function getUsersStats( req: Request, res: Response -): Promise { - try { - const users: IDBUser[] = await UserDatabaseService.getAll(); - if(!users?.length) return []; - const businesses: StatsUser[] = []; - const customers: StatsUser[] = []; - - await Promise.all(users.map(async (u: IDBUser): Promise => { - if(u.verifiedBusiness) { - const business = u.business; - const walletData: IWallet = await PublicServices.getWallet(business.dwollaId) - const user: StatsUser = { - firstName: business.owner.firstName, - lastName: business.owner.lastName, - email: u.email, - dwollaId: business.dwollaId, - balance: walletData.availableBalance, - lastLogin: 0, - walletAddress: business.walletAddress, - address: `${business.address1} ${business.address2}`, - type: 'business' - } - businesses.push(user); - } - if(u.verifiedCustomer) { - const customer = u.customer; - const walletData: IWallet = await PublicServices.getWallet(customer.dwollaId) - const user: StatsUser = { - firstName: customer.firstName, - lastName: customer.lastName, - email: u.email, - dwollaId: customer.dwollaId, - balance: walletData.availableBalance, - lastLogin: 0, - walletAddress: customer.walletAddress, - address: `${customer.address1} ${customer.address2}`, - type: 'customer' - } - customers.push(user); - } - return; - })) +): Promise { + try { + const users: IDBUser[] = await UserDatabaseService.getAll(); + if (!users?.length) return []; + const businesses: StatsUser[] = []; + const customers: StatsUser[] = []; - const all = customers.concat(businesses); - httpUtils.createHttpResponse(all, codes.OK, res); - } catch(err) { - httpUtils.serverError(err, res); - } -}; + await Promise.all( + users.map(async (u: IDBUser): Promise => { + if (u.verifiedBusiness) { + const business = u.business; + const walletData: IWallet = await PublicServices.getWallet( + business.dwollaId + ); + const user: StatsUser = { + firstName: business.owner.firstName, + lastName: business.owner.lastName, + email: u.email, + dwollaId: business.dwollaId, + balance: walletData.availableBalance, + lastLogin: 0, + walletAddress: business.walletAddress, + address: `${business.address1} ${business.address2}`, + type: "business", + }; + businesses.push(user); + } + if (u.verifiedCustomer) { + const customer = u.customer; + const walletData: IWallet = await PublicServices.getWallet( + customer.dwollaId + ); + const user: StatsUser = { + firstName: customer.firstName, + lastName: customer.lastName, + email: u.email, + dwollaId: customer.dwollaId, + balance: walletData.availableBalance, + lastLogin: 0, + walletAddress: customer.walletAddress, + address: `${customer.address1} ${customer.address2}`, + type: "customer", + }; + customers.push(user); + } + return; + }) + ); + const all = customers.concat(businesses); + httpUtils.createHttpResponse(all, codes.OK, res); + } catch (err) { + httpUtils.serverError(err, res); + } +} diff --git a/api/src/types/index.ts b/api/src/types/index.ts index 8617d357..1a8a9f7f 100644 --- a/api/src/types/index.ts +++ b/api/src/types/index.ts @@ -218,14 +218,13 @@ export type GenericDatabaseResponse = { }; export interface StatsUser { - firstName: string - lastName: string - email: string - dwollaId: string - balance: number - lastLogin: number - walletAddress: string - address: string - type: 'customer' | 'business' + firstName: string; + lastName: string; + email: string; + dwollaId: string; + balance: number; + lastLogin: number; + walletAddress: string; + address: string; + type: "customer" | "business"; } -