Skip to content

Commit

Permalink
Merge pull request #119 from humanity-cash/feature/users-stats
Browse files Browse the repository at this point in the history
Feature/users stats
  • Loading branch information
aaronmboyd authored Jan 10, 2022
2 parents 8236616 + 85dbd38 commit d34073f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
19 changes: 11 additions & 8 deletions api/src/database/service/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IBusinessDwollaId,
ICustomerDwollaId,
INewUserInput,
IDBUser,
} from "src/types";
import { User, User as UserSchema } from "../schema";
import { removeMongoMeta } from "../utils/index";
Expand Down Expand Up @@ -37,14 +38,16 @@ export async function get<T>(filter: any): Promise<T> {
return removeMongoMeta(response?.toObject());
}

export async function getAll(): Promise<IDBUser[]> {
const response = await User.find();
return response?.length > 0
? response.map((doc) => removeMongoMeta(doc.toObject()))
: [];
}

export async function getBusinesses(): Promise<Business[]> {
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)
: [];
}
64 changes: 64 additions & 0 deletions api/src/router/stats/controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request, Response } from "express";
import * as PublicServices from "src/service/PublicService";
import {
getFundingStatus,
getDeposits,
Expand All @@ -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;

Expand Down Expand Up @@ -62,3 +67,62 @@ export async function getAllTransfers(
httpUtils.serverError(err, res);
}
}

export async function getUsersStats(
req: Request,
res: Response
): Promise<StatsUser[]> {
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<StatsUser[]> => {
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);
}
}
1 change: 1 addition & 0 deletions api/src/router/stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 14 additions & 0 deletions api/src/test/Operator.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
13 changes: 13 additions & 0 deletions api/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,23 @@ export type GenericDatabaseResponse<T, E = string> = {
error?: E;
};

export interface StatsUser {
firstName: string;
lastName: string;
email: string;
dwollaId: string;
balance: number;
lastLogin: number;
walletAddress: string;
address: string;
type: "customer" | "business";
}

export interface HomeScreenContentLink {
url: string;
text: string;
}

export interface HomeScreenContent {
contentType:
| "Pictures"
Expand Down

0 comments on commit d34073f

Please sign in to comment.