targo-backend/src/identity-and-account/oauth-sessions/services/oauth-sessions.service.ts

87 lines
2.7 KiB
TypeScript

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<OAuthSessions> {
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<OAuthSessions[]> {
return this.prisma.oAuthSessions.findMany({
include: { user: true },
});
}
async findOne(id: string): Promise<OAuthSessions> {
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<OAuthSessions> {
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<OAuthSessions> {
await this.findOne(id);
return this.prisma.oAuthSessions.delete({ where: { id }});
}
}