chore: update biome configuration and enhance error handling in schema files

- Enabled useIgnoreFile in biome.json for better file management.
- Updated various correctness and style rules in biome.json to enforce stricter coding standards.
- Added new biome lint command in package.json for improved code quality checks.
- Refactored error handling in multiple schema files to use consistent error throwing patterns, enhancing readability and maintainability.
- Improved user authentication checks across various schemas to ensure proper access control.
This commit is contained in:
2024-12-08 21:01:26 +07:00
parent 10e20092ab
commit 45dca51990
17 changed files with 430 additions and 159 deletions

View File

@@ -171,9 +171,15 @@ 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) throw new Error('Unauthorized')
if (ctx.http.me.role !== Role.CUSTOMER) throw new Error('Only customers can access this data')
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')
}
// calculate analytic
const activeServiceCount = await this.prisma.order.count({
where: {
@@ -217,9 +223,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) throw new Error('Unauthorized')
if (ctx.http.me.role !== Role.CENTER_MENTOR) throw new Error('Only center mentors can access this data')
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')
}
// calculate analytic
return {
userId: ctx.http.me.id,
@@ -230,16 +242,24 @@ 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) throw new Error('Unauthorized')
if (ctx.http.me.role !== Role.CENTER_OWNER) throw new Error('Only center owners can access this data')
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')
}
// get center by owner id
const center = await this.prisma.center.findUnique({
where: {
centerOwnerId: ctx.http.me.id,
},
})
if (!center) throw new Error('Center not found')
if (!center) {
throw new Error('Center not found')
}
// calculate analytic
// active mentor include center owner
@@ -277,7 +297,9 @@ export class AnalyticSchema extends PothosSchema {
const service = await this.prisma.service.findUnique({
where: { id: order.serviceId },
})
if (!service) continue
if (!service) {
continue
}
const commission = service.commission
const actualRevenue = (order.total || 0) - (order.total || 0) * commission
revenue += actualRevenue
@@ -318,10 +340,15 @@ 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) throw new Error('Unauthorized')
if (ctx.http.me.role !== Role.ADMIN && ctx.http.me.role !== Role.MODERATOR)
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) {
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({
where: {
@@ -428,7 +455,9 @@ export class AnalyticSchema extends PothosSchema {
const service = await this.prisma.service.findUnique({
where: { id: order.serviceId },
})
if (!service) continue
if (!service) {
continue
}
const commission = service.commission
const actualRevenue = (order.total || 0) - (order.total || 0) * commission
revenue += actualRevenue