update sth :)

This commit is contained in:
2024-11-26 17:55:34 +07:00
parent 8b01df2111
commit 1062f5944d
4 changed files with 52 additions and 25 deletions

View File

@@ -1,11 +1,6 @@
import { Inject, Injectable } from '@nestjs/common'
import { OrderStatus, Prisma, Role, ServiceStatus } from '@prisma/client'
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos'
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
import { Builder } from 'src/Graphql/graphql.builder'
import { PrismaService } from 'src/Prisma/prisma.service'
@@ -125,6 +120,12 @@ export class AnalyticSchema extends PothosSchema {
totalMentorCount: t.int({
description: 'The total number of mentors.',
}),
totalServiceCount: t.int({
description: 'The total number of services.',
}),
totalWorkshopCount: t.int({
description: 'The total number of workshops.',
}),
revenue: t.int({
description: 'The total revenue.',
}),
@@ -172,8 +173,7 @@ export class AnalyticSchema extends PothosSchema {
resolve: async (_parent, _args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http.me) throw new Error('Unauthorized')
if (ctx.http.me.role !== Role.CUSTOMER)
throw new Error('Only customers can access this data')
if (ctx.http.me.role !== Role.CUSTOMER) throw new Error('Only customers can access this data')
// calculate analytic
const activeServiceCount = await this.prisma.order.count({
where: {
@@ -219,8 +219,7 @@ export class AnalyticSchema extends PothosSchema {
resolve: async (_parent, _args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http.me) throw new Error('Unauthorized')
if (ctx.http.me.role !== Role.CENTER_MENTOR)
throw new Error('Only center mentors can access this data')
if (ctx.http.me.role !== Role.CENTER_MENTOR) throw new Error('Only center mentors can access this data')
// calculate analytic
return {
userId: ctx.http.me.id,
@@ -233,8 +232,7 @@ export class AnalyticSchema extends PothosSchema {
resolve: async (_parent, _args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http.me) throw new Error('Unauthorized')
if (ctx.http.me.role !== Role.CENTER_OWNER)
throw new Error('Only center owners can access this data')
if (ctx.http.me.role !== Role.CENTER_OWNER) throw new Error('Only center owners can access this data')
// get center by owner id
const center = await this.prisma.center.findUnique({
where: {
@@ -281,8 +279,7 @@ export class AnalyticSchema extends PothosSchema {
})
if (!service) continue
const commission = service.commission
const actualRevenue =
(order.total || 0) - (order.total || 0) * commission
const actualRevenue = (order.total || 0) - (order.total || 0) * commission
revenue += actualRevenue
}
return {
@@ -323,10 +320,7 @@ export class AnalyticSchema extends PothosSchema {
resolve: async (_parent, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http.me) throw new Error('Unauthorized')
if (
ctx.http.me.role !== Role.ADMIN &&
ctx.http.me.role !== Role.MODERATOR
)
if (ctx.http.me.role !== Role.ADMIN && ctx.http.me.role !== Role.MODERATOR)
throw new Error('Only admins and moderators can access this data')
// calculate analytic for services sorted by args.serviceSortBy and args.timeframes
const topServices = await this.prisma.service.findMany({
@@ -411,13 +405,17 @@ export class AnalyticSchema extends PothosSchema {
status: ServiceStatus.REJECTED,
},
})
// get total workshop count
const totalWorkshopCount = await this.prisma.workshop.count()
// get total order count
const totalOrderCount = await this.prisma.order.count()
// get total service count
const totalServiceCount = await this.prisma.service.count()
// get revenue
let revenue = 0
// query all orders of services in all centers in the past args.timeframes and calculate actual revenue of each order by convert commission percentage to float
// convert args.timeframes to number of days
const timeframes = DateTimeUtils.subtractDaysFromTimeframe(
args.timeframes,
)
const timeframes = DateTimeUtils.subtractDaysFromTimeframe(args.timeframes)
const orders = await this.prisma.order.findMany({
where: {
status: OrderStatus.PAID,
@@ -432,8 +430,7 @@ export class AnalyticSchema extends PothosSchema {
})
if (!service) continue
const commission = service.commission
const actualRevenue =
(order.total || 0) - (order.total || 0) * commission
const actualRevenue = (order.total || 0) - (order.total || 0) * commission
revenue += actualRevenue
}
// return analytic
@@ -449,6 +446,9 @@ export class AnalyticSchema extends PothosSchema {
revenue: revenue,
approvedServiceCount: approvedServiceCount,
rejectedServiceCount: rejectedServiceCount,
totalWorkshopCount: totalWorkshopCount,
totalOrderCount: totalOrderCount,
totalServiceCount: totalServiceCount,
updatedAt: DateTimeUtils.now(),
}
},