refactor source code

This commit is contained in:
2024-10-29 17:42:54 +07:00
parent 3b23d9e0b7
commit 152bb50da8
83 changed files with 8473 additions and 7577 deletions

View File

@@ -1,20 +1,20 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common'
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { OrderStatus } from '@prisma/client';
} from '@smatch-corp/nestjs-pothos'
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { OrderStatus } from '@prisma/client'
@Injectable()
export class OrderSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
) {
super();
super()
}
// Types section
@@ -69,7 +69,7 @@ export class OrderSchema extends PothosSchema {
description: 'The schedule for the order.',
}),
}),
});
})
}
@Pothos()
@@ -88,7 +88,7 @@ export class OrderSchema extends PothosSchema {
skip: args.skip ?? undefined,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
});
})
},
}),
order: t.prismaField({
@@ -99,10 +99,10 @@ export class OrderSchema extends PothosSchema {
return await this.prisma.order.findUnique({
...query,
where: args.where,
});
})
},
}),
}));
}))
// mutation section
this.builder.mutationFields((t) => ({
createOrder: t.prismaField({
@@ -129,18 +129,17 @@ export class OrderSchema extends PothosSchema {
const order = await prisma.order.create({
...query,
data: args.data,
});
})
// check if service is valid
if (!args.data.service.connect) {
throw new Error('Service not found');
throw new Error('Service not found')
}
// check if service price is free
if (args.data.service.connect.price === 0) {
return order;
return order
}
// generate payment code by prefix 'EPESS' + 6 hex digits
const paymentCode =
'EPESS' + Math.random().toString(16).slice(2, 8);
const paymentCode = 'EPESS' + Math.random().toString(16).slice(2, 8)
// create payment
await prisma.payment.create({
data: {
@@ -149,9 +148,9 @@ export class OrderSchema extends PothosSchema {
paymentCode: paymentCode,
expiredAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
},
});
return order;
});
})
return order
})
},
}),
deleteOrder: t.prismaField({
@@ -167,7 +166,7 @@ export class OrderSchema extends PothosSchema {
return await this.prisma.order.delete({
...query,
where: args.where,
});
})
},
}),
updateOrder: t.prismaField({
@@ -191,9 +190,9 @@ export class OrderSchema extends PothosSchema {
...query,
data: args.data,
where: args.where,
});
})
},
}),
}));
}))
}
}