import { Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from 'src/prisma/prisma.service'; import { CreateOauthSessionDto } from '../dtos/create-oauth-session.dto'; import { OAuthSessions } from '@prisma/client'; import { UpdateOauthSessionDto } from '../dtos/update-oauth-session.dto'; @Injectable() export class OauthSessionsService { constructor(private readonly prisma: PrismaService) {} async create(dto: CreateOauthSessionDto): Promise { const { user_id, application, access_token, refresh_token, sid, access_token_expiry, refresh_token_expiry, scopes, } = dto; return this.prisma.oAuthSessions.create({ data: { user_id, application, access_token, refresh_token, sid, access_token_expiry, refresh_token_expiry, scopes, }, include: { user: true }, }); } findAll(): Promise { return this.prisma.oAuthSessions.findMany({ include: { user: true }, }); } async findOne(id: string): Promise { const token = await this.prisma.oAuthSessions.findUnique({ where: { id }, include: { user: true }, }); if(!token) { throw new NotFoundException(`token #${ id } not found`); } return token; } async update(id: string, dto: UpdateOauthSessionDto): Promise { await this.findOne(id); const { user_id, application, access_token, refresh_token, access_token_expiry, refresh_token_expiry, scopes, } = dto; return this.prisma.oAuthSessions.update({ where: { id }, data: { ...(user_id !== undefined && { user_id }), ...(application !== undefined && { application }), ...(access_token !== undefined && { access_token }), ...(refresh_token !== undefined && { refresh_token }), ...(access_token_expiry !== undefined && { access_token_expiry }), ...(refresh_token_expiry !== undefined && { refresh_token_expiry }), ...(scopes !== undefined && { scopes }), }, include: { user: true }, }); } async remove(id: string): Promise { await this.findOne(id); return this.prisma.oAuthSessions.delete({ where: { id }}); } }