Add manager bonus account detail
This commit is contained in:
107
src/resolvers.js
107
src/resolvers.js
@@ -76,6 +76,23 @@ function formatPercent(value) {
|
|||||||
return Number(value).toFixed(2).replace(/\.?0+$/, '');
|
return Number(value).toFixed(2).replace(/\.?0+$/, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapManagerReferralLink(link) {
|
||||||
|
return {
|
||||||
|
id: link.id,
|
||||||
|
referrerId: link.referrerId,
|
||||||
|
referrerName: link.referrer.fullName,
|
||||||
|
referrerEmail: link.referrer.email,
|
||||||
|
referrerCompanyName: link.referrer.counterpartyProfile?.companyName ?? null,
|
||||||
|
refereeId: link.refereeId,
|
||||||
|
refereeName: link.referee.fullName,
|
||||||
|
refereeEmail: link.referee.email,
|
||||||
|
refereeCompanyName: link.referee.counterpartyProfile?.companyName ?? null,
|
||||||
|
createdById: link.createdById,
|
||||||
|
bonusPercent: Number(link.bonusPercent),
|
||||||
|
createdAt: link.createdAt,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function createReferralBonusTransaction(prisma, order) {
|
async function createReferralBonusTransaction(prisma, order) {
|
||||||
const referralLink = await prisma.referralLink.findFirst({
|
const referralLink = await prisma.referralLink.findFirst({
|
||||||
where: {
|
where: {
|
||||||
@@ -728,20 +745,7 @@ export const resolvers = {
|
|||||||
orderBy: { createdAt: 'desc' },
|
orderBy: { createdAt: 'desc' },
|
||||||
});
|
});
|
||||||
|
|
||||||
return links.map((link) => ({
|
return links.map(mapManagerReferralLink);
|
||||||
id: link.id,
|
|
||||||
referrerId: link.referrerId,
|
|
||||||
referrerName: link.referrer.fullName,
|
|
||||||
referrerEmail: link.referrer.email,
|
|
||||||
referrerCompanyName: link.referrer.counterpartyProfile?.companyName ?? null,
|
|
||||||
refereeId: link.refereeId,
|
|
||||||
refereeName: link.referee.fullName,
|
|
||||||
refereeEmail: link.referee.email,
|
|
||||||
refereeCompanyName: link.referee.counterpartyProfile?.companyName ?? null,
|
|
||||||
createdById: link.createdById,
|
|
||||||
bonusPercent: Number(link.bonusPercent),
|
|
||||||
createdAt: link.createdAt,
|
|
||||||
}));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
managerBonusBalances: async (_, __, context) => {
|
managerBonusBalances: async (_, __, context) => {
|
||||||
@@ -801,6 +805,81 @@ export const resolvers = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
managerBonusAccount: async (_, { userId }, context) => {
|
||||||
|
const manager = requireManagerAccess(context);
|
||||||
|
await assertManagerCanAccessUser(context.prisma, manager, userId);
|
||||||
|
|
||||||
|
const [user, links, transactions, pendingWithdrawals] = await Promise.all([
|
||||||
|
context.prisma.user.findUnique({
|
||||||
|
where: { id: userId },
|
||||||
|
include: {
|
||||||
|
counterpartyProfile: {
|
||||||
|
select: {
|
||||||
|
companyName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
context.prisma.referralLink.findMany({
|
||||||
|
where: { referrerId: userId },
|
||||||
|
include: {
|
||||||
|
referrer: {
|
||||||
|
include: {
|
||||||
|
counterpartyProfile: {
|
||||||
|
select: {
|
||||||
|
companyName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
referee: {
|
||||||
|
include: {
|
||||||
|
counterpartyProfile: {
|
||||||
|
select: {
|
||||||
|
companyName: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: { createdAt: 'desc' },
|
||||||
|
}),
|
||||||
|
context.prisma.bonusTransaction.findMany({
|
||||||
|
where: { userId },
|
||||||
|
orderBy: { createdAt: 'desc' },
|
||||||
|
}),
|
||||||
|
context.prisma.rewardWithdrawalRequest.findMany({
|
||||||
|
where: {
|
||||||
|
requesterId: userId,
|
||||||
|
status: 'PENDING',
|
||||||
|
},
|
||||||
|
orderBy: { createdAt: 'desc' },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new Error('User was not found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const earnedAmount = transactions.reduce((acc, tx) => acc + Number(tx.amount), 0);
|
||||||
|
const pendingWithdrawalAmount = pendingWithdrawals.reduce((acc, withdrawal) => acc + Number(withdrawal.amount), 0);
|
||||||
|
|
||||||
|
return {
|
||||||
|
userId: user.id,
|
||||||
|
email: user.email,
|
||||||
|
fullName: user.fullName,
|
||||||
|
companyName: user.counterpartyProfile?.companyName ?? null,
|
||||||
|
balance: earnedAmount - pendingWithdrawalAmount,
|
||||||
|
earnedAmount,
|
||||||
|
pendingWithdrawalAmount,
|
||||||
|
transactionsCount: transactions.length,
|
||||||
|
referralsCount: links.length,
|
||||||
|
referralLinks: links.map(mapManagerReferralLink),
|
||||||
|
transactions,
|
||||||
|
pendingWithdrawals,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
managerWithdrawalRequests: async (_, { status }, context) => {
|
managerWithdrawalRequests: async (_, { status }, context) => {
|
||||||
const manager = requireManagerAccess(context);
|
const manager = requireManagerAccess(context);
|
||||||
const managedUsersWhere = await getManagedClientUserWhere(context.prisma, manager);
|
const managedUsersWhere = await getManagedClientUserWhere(context.prisma, manager);
|
||||||
|
|||||||
@@ -327,6 +327,21 @@ type ManagerBonusBalance {
|
|||||||
transactionsCount: Int!
|
transactionsCount: Int!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ManagerBonusAccount {
|
||||||
|
userId: ID!
|
||||||
|
email: String!
|
||||||
|
fullName: String!
|
||||||
|
companyName: String
|
||||||
|
balance: Float!
|
||||||
|
earnedAmount: Float!
|
||||||
|
pendingWithdrawalAmount: Float!
|
||||||
|
transactionsCount: Int!
|
||||||
|
referralsCount: Int!
|
||||||
|
referralLinks: [ManagerReferralLink!]!
|
||||||
|
transactions: [BonusTransaction!]!
|
||||||
|
pendingWithdrawals: [RewardWithdrawalRequest!]!
|
||||||
|
}
|
||||||
|
|
||||||
type ManagerWithdrawalRequest {
|
type ManagerWithdrawalRequest {
|
||||||
id: ID!
|
id: ID!
|
||||||
requesterId: ID!
|
requesterId: ID!
|
||||||
@@ -358,6 +373,7 @@ type Query {
|
|||||||
managerOrders(status: OrderStatus, customerId: ID): [Order!]!
|
managerOrders(status: OrderStatus, customerId: ID): [Order!]!
|
||||||
managerReferralLinks: [ManagerReferralLink!]!
|
managerReferralLinks: [ManagerReferralLink!]!
|
||||||
managerBonusBalances: [ManagerBonusBalance!]!
|
managerBonusBalances: [ManagerBonusBalance!]!
|
||||||
|
managerBonusAccount(userId: ID!): ManagerBonusAccount!
|
||||||
managerWithdrawalRequests(status: WithdrawalStatus): [ManagerWithdrawalRequest!]!
|
managerWithdrawalRequests(status: WithdrawalStatus): [ManagerWithdrawalRequest!]!
|
||||||
registrationRequests(status: RegistrationStatus): [RegistrationRequest!]!
|
registrationRequests(status: RegistrationStatus): [RegistrationRequest!]!
|
||||||
referralStats: ReferralStats!
|
referralStats: ReferralStats!
|
||||||
|
|||||||
Reference in New Issue
Block a user