AI da dat ten cho dong song

This commit is contained in:
2024-10-26 11:10:14 +07:00
parent 2b6d3869f9
commit 48305a3948
11 changed files with 2999 additions and 63 deletions

View File

@@ -9,12 +9,13 @@ import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { clerkClient } from '@clerk/express';
import { UnauthorizedException } from '@nestjs/common';
import { MailService } from '../Mail/mail.service';
@Injectable()
export class UserSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly mailService: MailService,
) {
super();
}
@@ -123,22 +124,26 @@ export class UserSchema extends PothosSchema {
},
}),
me: t.prismaField({
description: 'Retrieve the current user.',
description: 'Retrieve the current user by token.',
type: this.user(),
resolve: async (query, root, args, ctx, info) => {
const sessionCookie = ctx.req.headers.cookie
?.split('; ')
.find((row) => row.startsWith('__session='))
?.split('=')[1];
if (!sessionCookie)
// get session id from X-Session-Id
const sessionId = ctx.req.headers['x-session-id'];
if (!sessionId)
throw new UnauthorizedException({
message: 'No session cookie found',
message: 'No session ID found',
});
const session = await clerkClient.sessions.getSession(sessionCookie);
// verify the token
const session = await clerkClient.sessions.getSession(
sessionId as string,
);
if (!session) throw new UnauthorizedException();
return await this.prisma.user.findUnique({
const user = await this.prisma.user.findUnique({
where: { id: session.userId },
});
if (!user) throw new UnauthorizedException();
ctx.me = user;
return user;
},
}),
@@ -227,6 +232,17 @@ export class UserSchema extends PothosSchema {
// });
// },
// }),
sendEmailTest: t.field({
type: 'String',
args: {
to: t.arg({ type: 'String', required: true }),
},
resolve: async (_parent, args, _context, _info) => {
await this.mailService.sendEmail(args.to, 'Test', 'Test');
return 'Email sent';
},
}),
}));
}
}