chore: combine context
This commit is contained in:
@@ -171,19 +171,16 @@ export class AnalyticSchema extends PothosSchema {
|
||||
type: this.customerAnalytic(),
|
||||
description: 'Retrieve a single customer analytic.',
|
||||
resolve: async (_parent, _args, ctx, _info) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
if (!ctx.http.me) {
|
||||
if (!ctx.me) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
if (ctx.http.me.role !== Role.CUSTOMER) {
|
||||
if (ctx.me.role !== Role.CUSTOMER) {
|
||||
throw new Error('Only customers can access this data')
|
||||
}
|
||||
// calculate analytic
|
||||
const activeServiceCount = await this.prisma.order.count({
|
||||
where: {
|
||||
userId: ctx.http.me.id,
|
||||
userId: ctx.me.id,
|
||||
status: OrderStatus.PAID,
|
||||
schedule: {
|
||||
dates: {
|
||||
@@ -198,12 +195,12 @@ export class AnalyticSchema extends PothosSchema {
|
||||
})
|
||||
const totalServiceCount = await this.prisma.order.count({
|
||||
where: {
|
||||
userId: ctx.http.me.id,
|
||||
userId: ctx.me.id,
|
||||
},
|
||||
})
|
||||
const totalSpent = await this.prisma.order.aggregate({
|
||||
where: {
|
||||
userId: ctx.http.me.id,
|
||||
userId: ctx.me.id,
|
||||
status: OrderStatus.PAID,
|
||||
},
|
||||
_sum: {
|
||||
@@ -211,7 +208,7 @@ export class AnalyticSchema extends PothosSchema {
|
||||
},
|
||||
})
|
||||
return {
|
||||
userId: ctx.http.me.id,
|
||||
userId: ctx.me.id,
|
||||
activeServiceCount: activeServiceCount,
|
||||
totalServiceCount: totalServiceCount,
|
||||
totalSpent: totalSpent._sum.total,
|
||||
@@ -223,18 +220,15 @@ export class AnalyticSchema extends PothosSchema {
|
||||
type: this.mentorAnalytic(),
|
||||
description: 'Retrieve a single mentor analytic.',
|
||||
resolve: async (_parent, _args, ctx, _info) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
if (!ctx.http.me) {
|
||||
if (!ctx.me) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
if (ctx.http.me.role !== Role.CENTER_MENTOR) {
|
||||
if (ctx.me.role !== Role.CENTER_MENTOR) {
|
||||
throw new Error('Only center mentors can access this data')
|
||||
}
|
||||
// calculate analytic
|
||||
return {
|
||||
userId: ctx.http.me.id,
|
||||
userId: ctx.me.id,
|
||||
}
|
||||
},
|
||||
}),
|
||||
@@ -242,19 +236,16 @@ export class AnalyticSchema extends PothosSchema {
|
||||
type: this.centerAnalytic(),
|
||||
description: 'Retrieve a single center analytic.',
|
||||
resolve: async (_parent, _args, ctx, _info) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
if (!ctx.http.me) {
|
||||
if (!ctx.me) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
if (ctx.http.me.role !== Role.CENTER_OWNER) {
|
||||
if (ctx.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: {
|
||||
centerOwnerId: ctx.http.me.id,
|
||||
centerOwnerId: ctx.me.id,
|
||||
},
|
||||
})
|
||||
if (!center) {
|
||||
@@ -266,7 +257,7 @@ export class AnalyticSchema extends PothosSchema {
|
||||
const activeMentorCount = await this.prisma.user.count({
|
||||
where: {
|
||||
center: {
|
||||
centerOwnerId: ctx.http.me.id,
|
||||
centerOwnerId: ctx.me.id,
|
||||
},
|
||||
banned: false,
|
||||
},
|
||||
@@ -340,13 +331,10 @@ export class AnalyticSchema extends PothosSchema {
|
||||
},
|
||||
description: 'Retrieve a single platform analytic.',
|
||||
resolve: async (_parent, args, ctx, _info) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
if (!ctx.http.me) {
|
||||
if (!ctx.me) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
if (ctx.http.me.role !== Role.ADMIN && ctx.http.me.role !== Role.MODERATOR) {
|
||||
if (ctx.me.role !== Role.ADMIN && ctx.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
|
||||
|
||||
Reference in New Issue
Block a user