update service status

This commit is contained in:
2024-10-23 15:43:50 +07:00
parent 7e55f4093b
commit 2872ac69ef
14 changed files with 212 additions and 32 deletions

View File

@@ -8,6 +8,9 @@ import {
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { clerkClient } from '@clerk/express';
import type { Session } from '@clerk/express';
import { UnauthorizedException } from '@nestjs/common';
@Injectable()
export class UserSchema extends PothosSchema {
constructor(
@@ -89,17 +92,51 @@ export class UserSchema extends PothosSchema {
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
// me: t.prismaField({
// description: 'Retrieve the current user.',
// type: this.user(),
// resolve: async (query, root, args, ctx, info) => {
// const sessionId = ctx.req.headers.sessionId;
// const userId = await clerkClient.users.getUser()
// return await this.prisma.user.findUnique({
// where: { id: ctx.req.headers.authorization },
// });
// },
// }),
session: t.field({
type: 'Json',
args: {
sessionId: t.arg({ type: 'String', required: true }),
},
resolve: async (_, { sessionId }) => {
const session = await clerkClient.sessions.getSession(sessionId);
return JSON.parse(JSON.stringify(session));
},
}),
newSession: t.field({
type: 'String',
args: {
userId: t.arg({
type: 'String',
required: true,
}),
},
resolve: async (_, { userId }) => {
const session = await clerkClient.signInTokens.createSignInToken({
userId,
expiresInSeconds: 60 * 60 * 24,
});
return session.id;
},
}),
me: t.prismaField({
description: 'Retrieve the current user.',
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)
throw new UnauthorizedException({
message: 'No session cookie found',
});
const session = await clerkClient.sessions.getSession(sessionCookie);
if (!session) throw new UnauthorizedException();
return await this.prisma.user.findUnique({
where: { id: session.userId },
});
},
}),
users: t.prismaField({
description: