diff --git a/src/interceptors/audit-log.interceptor.ts b/src/interceptors/audit-log.interceptor.ts index b0228c761..2ded26ec1 100644 --- a/src/interceptors/audit-log.interceptor.ts +++ b/src/interceptors/audit-log.interceptor.ts @@ -11,13 +11,13 @@ import type { Prisma } from '@prisma/client'; import { getClientIp } from 'request-ip'; import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; +import { UAParser } from 'ua-parser-js'; import { GROUP_NOT_FOUND } from '../errors/errors.constants'; import { STAART_AUDIT_LOG_DATA } from '../modules/audit-logs/audit-log.constants'; import { UserRequest } from '../modules/auth/auth.interface'; +import { WebhooksService } from '../modules/webhooks/webhooks.service'; import { GeolocationService } from '../providers/geolocation/geolocation.service'; import { PrismaService } from '../providers/prisma/prisma.service'; -import { WebhooksService } from '../modules/webhooks/webhooks.service'; -import { UAParser } from 'ua-parser-js'; @Injectable() export class AuditLogger implements NestInterceptor { @@ -48,7 +48,7 @@ export class AuditLogger implements NestInterceptor { const userAgent = request.get('user-agent'); const ua = new UAParser(userAgent); for await (const event of auditLog) { - const data: Prisma.auditLogsCreateInput = { + const data: Prisma.AuditLogCreateInput = { event, city: location?.city?.names?.en, region: location?.subdivisions?.pop()?.names?.en, @@ -67,7 +67,7 @@ export class AuditLogger implements NestInterceptor { if (request.user.id && request.user.type === 'user') data.user = { connect: { id: request.user.id } }; if (groupId) data.group = { connect: { id: groupId } }; - await this.prisma.auditLogs.create({ data }); + await this.prisma.auditLog.create({ data }); this.webhooksService.triggerWebhook(groupId, event); } } diff --git a/src/modules/api-keys/api-keys-group.controller.ts b/src/modules/api-keys/api-keys-group.controller.ts index cfaa68307..894490495 100644 --- a/src/modules/api-keys/api-keys-group.controller.ts +++ b/src/modules/api-keys/api-keys-group.controller.ts @@ -10,14 +10,14 @@ import { Put, Query, } from '@nestjs/common'; -import { apiKeys } from '@prisma/client'; +import { ApiKey } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { AuditLog } from '../audit-logs/audit-log.decorator'; import { Scopes } from '../auth/scope.decorator'; -import { Expose } from '../../providers/prisma/prisma.interface'; import { CreateApiKeyDto, ReplaceApiKeyDto, @@ -35,7 +35,7 @@ export class ApiKeyGroupController { async create( @Param('groupId', ParseIntPipe) groupId: number, @Body() data: CreateApiKeyDto, - ): Promise> { + ): Promise> { return this.apiKeysService.createApiKeyForGroup(groupId, data); } @@ -48,7 +48,7 @@ export class ApiKeyGroupController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.apiKeysService.getApiKeysForGroup(groupId, { skip, take, @@ -71,7 +71,7 @@ export class ApiKeyGroupController { async get( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.getApiKeyForGroup(groupId, Number(id)); } @@ -82,7 +82,7 @@ export class ApiKeyGroupController { @Body() data: UpdateApiKeyDto, @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.updateApiKeyForGroup(groupId, Number(id), data); } @@ -93,7 +93,7 @@ export class ApiKeyGroupController { @Body() data: ReplaceApiKeyDto, @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.updateApiKeyForGroup(groupId, Number(id), data); } @@ -103,7 +103,7 @@ export class ApiKeyGroupController { async remove( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.deleteApiKeyForGroup(groupId, Number(id)); } diff --git a/src/modules/api-keys/api-keys-user.controller.ts b/src/modules/api-keys/api-keys-user.controller.ts index 2b715ee80..218440ff6 100644 --- a/src/modules/api-keys/api-keys-user.controller.ts +++ b/src/modules/api-keys/api-keys-user.controller.ts @@ -10,14 +10,14 @@ import { Put, Query, } from '@nestjs/common'; -import { apiKeys } from '@prisma/client'; +import { ApiKey } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { AuditLog } from '../audit-logs/audit-log.decorator'; import { Scopes } from '../auth/scope.decorator'; -import { Expose } from '../../providers/prisma/prisma.interface'; import { CreateApiKeyDto, ReplaceApiKeyDto, @@ -35,7 +35,7 @@ export class ApiKeyUserController { async create( @Param('userId', ParseIntPipe) userId: number, @Body() data: CreateApiKeyDto, - ): Promise> { + ): Promise> { return this.apiKeysService.createApiKeyForUser(userId, data); } @@ -48,7 +48,7 @@ export class ApiKeyUserController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.apiKeysService.getApiKeysForUser(userId, { skip, take, @@ -71,7 +71,7 @@ export class ApiKeyUserController { async get( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.getApiKeyForUser(userId, Number(id)); } @@ -82,7 +82,7 @@ export class ApiKeyUserController { @Body() data: UpdateApiKeyDto, @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.updateApiKeyForUser(userId, Number(id), data); } @@ -93,7 +93,7 @@ export class ApiKeyUserController { @Body() data: ReplaceApiKeyDto, @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.updateApiKeyForUser(userId, Number(id), data); } @@ -103,7 +103,7 @@ export class ApiKeyUserController { async remove( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.apiKeysService.deleteApiKeyForUser(userId, Number(id)); } diff --git a/src/modules/api-keys/api-keys.service.ts b/src/modules/api-keys/api-keys.service.ts index 7f037f2cd..798655182 100644 --- a/src/modules/api-keys/api-keys.service.ts +++ b/src/modules/api-keys/api-keys.service.ts @@ -4,22 +4,22 @@ import { UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { apiKeys } from '@prisma/client'; -import type { Prisma, InputJsonValue, JsonValue } from '@prisma/client'; +import type { InputJsonValue, JsonValue, Prisma } from '@prisma/client'; +import { ApiKey } from '@prisma/client'; import QuickLRU from 'quick-lru'; import { API_KEY_NOT_FOUND, UNAUTHORIZED_RESOURCE, } from '../../errors/errors.constants'; +import { ElasticSearchService } from '../../providers/elasticsearch/elasticsearch.service'; import { Expose } from '../../providers/prisma/prisma.interface'; import { PrismaService } from '../../providers/prisma/prisma.service'; -import { StripeService } from '../stripe/stripe.service'; import { TokensService } from '../../providers/tokens/tokens.service'; -import { ElasticSearchService } from '../../providers/elasticsearch/elasticsearch.service'; +import { StripeService } from '../stripe/stripe.service'; @Injectable() export class ApiKeysService { - private lru = new QuickLRU({ + private lru = new QuickLRU({ maxSize: this.configService.get('caching.apiKeyLruSize') ?? 100, }); @@ -33,21 +33,21 @@ export class ApiKeysService { async createApiKeyForGroup( groupId: number, - data: Omit, 'group'>, - ): Promise { + data: Omit, 'group'>, + ): Promise { const apiKey = this.tokensService.generateUuid(); data.scopes = this.cleanScopesForGroup(groupId, data.scopes); - return this.prisma.apiKeys.create({ + return this.prisma.apiKey.create({ data: { ...data, apiKey, group: { connect: { id: groupId } } }, }); } async createApiKeyForUser( userId: number, - data: Omit, 'user'>, - ): Promise { + data: Omit, 'user'>, + ): Promise { const apiKey = this.tokensService.generateUuid(); data.scopes = this.cleanScopesForUser(userId, data.scopes); - return this.prisma.apiKeys.create({ + return this.prisma.apiKey.create({ data: { ...data, apiKey, user: { connect: { id: userId } } }, }); } @@ -57,183 +57,183 @@ export class ApiKeysService { params: { skip?: number; take?: number; - cursor?: Prisma.apiKeysWhereUniqueInput; - where?: Prisma.apiKeysWhereInput; - orderBy?: Prisma.apiKeysOrderByInput; + cursor?: Prisma.ApiKeyWhereUniqueInput; + where?: Prisma.ApiKeyWhereInput; + orderBy?: Prisma.ApiKeyOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const apiKeys = await this.prisma.apiKeys.findMany({ + const ApiKey = await this.prisma.apiKey.findMany({ skip, take, cursor, where: { ...where, group: { id: groupId } }, orderBy, }); - return apiKeys.map((group) => this.prisma.expose(group)); + return ApiKey.map((group) => this.prisma.expose(group)); } async getApiKeysForUser( userId: number, params: { skip?: number; take?: number; - cursor?: Prisma.apiKeysWhereUniqueInput; - where?: Prisma.apiKeysWhereInput; - orderBy?: Prisma.apiKeysOrderByInput; + cursor?: Prisma.ApiKeyWhereUniqueInput; + where?: Prisma.ApiKeyWhereInput; + orderBy?: Prisma.ApiKeyOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const apiKeys = await this.prisma.apiKeys.findMany({ + const ApiKey = await this.prisma.apiKey.findMany({ skip, take, cursor, where: { ...where, user: { id: userId } }, orderBy, }); - return apiKeys.map((user) => this.prisma.expose(user)); + return ApiKey.map((user) => this.prisma.expose(user)); } async getApiKeyForGroup( groupId: number, id: number, - ): Promise> { - const apiKey = await this.prisma.apiKeys.findUnique({ + ): Promise> { + const apiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!apiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (apiKey.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } - async getApiKeyForUser(userId: number, id: number): Promise> { - const apiKey = await this.prisma.apiKeys.findUnique({ + async getApiKeyForUser(userId: number, id: number): Promise> { + const apiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!apiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (apiKey.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } - async getApiKeyFromKey(key: string): Promise> { + async getApiKeyFromKey(key: string): Promise> { if (this.lru.has(key)) return this.lru.get(key); - const apiKey = await this.prisma.apiKeys.findFirst({ + const apiKey = await this.prisma.apiKey.findFirst({ where: { apiKey: key }, }); if (!apiKey) throw new NotFoundException(API_KEY_NOT_FOUND); this.lru.set(key, apiKey); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } async updateApiKeyForGroup( groupId: number, id: number, - data: Prisma.apiKeysUpdateInput, - ): Promise> { - const testApiKey = await this.prisma.apiKeys.findUnique({ + data: Prisma.ApiKeyUpdateInput, + ): Promise> { + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (testApiKey.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); data.scopes = this.cleanScopesForGroup(groupId, data.scopes); - const apiKey = await this.prisma.apiKeys.update({ + const apiKey = await this.prisma.apiKey.update({ where: { id }, data, }); this.lru.delete(testApiKey.apiKey); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } async updateApiKeyForUser( userId: number, id: number, - data: Prisma.apiKeysUpdateInput, - ): Promise> { - const testApiKey = await this.prisma.apiKeys.findUnique({ + data: Prisma.ApiKeyUpdateInput, + ): Promise> { + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (testApiKey.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); data.scopes = this.cleanScopesForUser(userId, data.scopes); - const apiKey = await this.prisma.apiKeys.update({ + const apiKey = await this.prisma.apiKey.update({ where: { id }, data, }); this.lru.delete(testApiKey.apiKey); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } async replaceApiKeyForGroup( groupId: number, id: number, - data: Prisma.apiKeysCreateInput, - ): Promise> { - const testApiKey = await this.prisma.apiKeys.findUnique({ + data: Prisma.ApiKeyCreateInput, + ): Promise> { + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (testApiKey.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); data.scopes = this.cleanScopesForGroup(groupId, data.scopes); - const apiKey = await this.prisma.apiKeys.update({ + const apiKey = await this.prisma.apiKey.update({ where: { id }, data, }); this.lru.delete(testApiKey.apiKey); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } async replaceApiKeyForUser( userId: number, id: number, - data: Prisma.apiKeysCreateInput, - ): Promise> { - const testApiKey = await this.prisma.apiKeys.findUnique({ + data: Prisma.ApiKeyCreateInput, + ): Promise> { + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (testApiKey.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); data.scopes = this.cleanScopesForUser(userId, data.scopes); - const apiKey = await this.prisma.apiKeys.update({ + const apiKey = await this.prisma.apiKey.update({ where: { id }, data, }); this.lru.delete(testApiKey.apiKey); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } async deleteApiKeyForGroup( groupId: number, id: number, - ): Promise> { - const testApiKey = await this.prisma.apiKeys.findUnique({ + ): Promise> { + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (testApiKey.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const apiKey = await this.prisma.apiKeys.delete({ + const apiKey = await this.prisma.apiKey.delete({ where: { id }, }); this.lru.delete(testApiKey.apiKey); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } async deleteApiKeyForUser( userId: number, id: number, - ): Promise> { - const testApiKey = await this.prisma.apiKeys.findUnique({ + ): Promise> { + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); if (testApiKey.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const apiKey = await this.prisma.apiKeys.delete({ + const apiKey = await this.prisma.apiKey.delete({ where: { id }, }); this.lru.delete(testApiKey.apiKey); - return this.prisma.expose(apiKey); + return this.prisma.expose(apiKey); } async getApiKeyLogsForGroup( @@ -245,7 +245,7 @@ export class ApiKeysService { where?: { after?: string }; }, ) { - const testApiKey = await this.prisma.apiKeys.findUnique({ + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); @@ -262,7 +262,7 @@ export class ApiKeysService { where?: { after?: string }; }, ) { - const testApiKey = await this.prisma.apiKeys.findUnique({ + const testApiKey = await this.prisma.apiKey.findUnique({ where: { id }, }); if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND); @@ -278,7 +278,7 @@ export class ApiKeysService { * they don't have access to anymore from that API key */ async removeUnauthorizedScopesForUser(userId: number): Promise { - const userApiKeys = await this.prisma.apiKeys.findMany({ + const userApiKeys = await this.prisma.apiKey.findMany({ where: { user: { id: userId } }, }); if (!userApiKeys.length) return; @@ -289,7 +289,7 @@ export class ApiKeysService { Object.keys(scopesAllowed).includes(i), ); if (currentScopes.length !== newScopes.length) - this.prisma.apiKeys.update({ + this.prisma.apiKey.update({ where: { id: apiKey.id }, data: { scopes: newScopes }, }); @@ -392,7 +392,7 @@ export class ApiKeysService { async getApiKeyScopesForGroup( groupId: number, ): Promise> { - const group = await this.prisma.groups.findUnique({ + const group = await this.prisma.group.findUnique({ where: { id: groupId }, select: { attributes: true }, }); @@ -405,7 +405,7 @@ export class ApiKeysService { scopes[`write-membership-*`] = 'Invite and update members'; scopes[`read-membership-*`] = 'Read members'; - for await (const membership of await this.prisma.memberships.findMany({ + for await (const membership of await this.prisma.membership.findMany({ where: { group: { id: groupId } }, select: { id: true, user: true }, })) { @@ -422,7 +422,7 @@ export class ApiKeysService { scopes[`write-api-key-*`] = 'Create and update API keys'; scopes[`read-api-key-*`] = 'Read API keys'; - for await (const apiKey of await this.prisma.apiKeys.findMany({ + for await (const apiKey of await this.prisma.apiKey.findMany({ where: { group: { id: groupId } }, select: { id: true, name: true, apiKey: true }, })) { @@ -439,7 +439,7 @@ export class ApiKeysService { scopes[`write-webhook-*`] = 'Create and update webhooks'; scopes[`read-webhook-*`] = 'Read webhooks'; - for await (const webhook of await this.prisma.webhooks.findMany({ + for await (const webhook of await this.prisma.webhook.findMany({ where: { group: { id: groupId } }, select: { id: true, url: true }, })) { @@ -492,7 +492,7 @@ export class ApiKeysService { scopes[`write-membership-*`] = 'Create new groups'; scopes[`read-membership-*`] = 'Read group memberships'; - for await (const membership of await this.prisma.memberships.findMany({ + for await (const membership of await this.prisma.membership.findMany({ where: { user: { id: userId } }, select: { id: true, group: true }, })) { @@ -509,7 +509,7 @@ export class ApiKeysService { scopes[`write-email-*`] = 'Create and update emails'; scopes[`read-email-*`] = 'Read emails'; - for await (const email of await this.prisma.emails.findMany({ + for await (const email of await this.prisma.email.findMany({ where: { user: { id: userId } }, select: { id: true, email: true }, })) { @@ -518,7 +518,7 @@ export class ApiKeysService { } scopes[`read-session-*`] = 'Read sessions'; - for await (const session of await this.prisma.sessions.findMany({ + for await (const session of await this.prisma.session.findMany({ where: { user: { id: userId } }, select: { id: true, browser: true }, })) { @@ -531,7 +531,7 @@ export class ApiKeysService { } scopes[`read-approved-subnet-*`] = 'Read approvedSubnets'; - for await (const subnet of await this.prisma.approvedSubnets.findMany({ + for await (const subnet of await this.prisma.approvedSubnet.findMany({ where: { user: { id: userId } }, select: { id: true, subnet: true }, })) { @@ -545,7 +545,7 @@ export class ApiKeysService { scopes[`write-api-key-*`] = 'Create and update API keys'; scopes[`read-api-key-*`] = 'Read API keys'; - for await (const apiKey of await this.prisma.apiKeys.findMany({ + for await (const apiKey of await this.prisma.apiKey.findMany({ where: { user: { id: userId } }, select: { id: true, name: true, apiKey: true }, })) { diff --git a/src/modules/approved-subnets/approved-subnets.controller.ts b/src/modules/approved-subnets/approved-subnets.controller.ts index d3424d662..e815aece6 100644 --- a/src/modules/approved-subnets/approved-subnets.controller.ts +++ b/src/modules/approved-subnets/approved-subnets.controller.ts @@ -6,12 +6,12 @@ import { ParseIntPipe, Query, } from '@nestjs/common'; -import { approvedSubnets } from '@prisma/client'; -import { Expose } from '../../providers/prisma/prisma.interface'; +import { ApprovedSubnet } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { Scopes } from '../auth/scope.decorator'; import { ApprovedSubnetsService } from './approved-subnets.service'; @@ -28,7 +28,7 @@ export class ApprovedSubnetController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.approvedSubnetsService.getApprovedSubnets(userId, { skip, take, @@ -43,7 +43,7 @@ export class ApprovedSubnetController { async get( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.approvedSubnetsService.getApprovedSubnet(userId, Number(id)); } @@ -52,7 +52,7 @@ export class ApprovedSubnetController { async remove( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.approvedSubnetsService.deleteApprovedSubnet(userId, Number(id)); } } diff --git a/src/modules/approved-subnets/approved-subnets.service.ts b/src/modules/approved-subnets/approved-subnets.service.ts index f76a9c4ce..c08d5366e 100644 --- a/src/modules/approved-subnets/approved-subnets.service.ts +++ b/src/modules/approved-subnets/approved-subnets.service.ts @@ -4,16 +4,16 @@ import { UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { approvedSubnets } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { ApprovedSubnet } from '@prisma/client'; import { compare, hash } from 'bcrypt'; import anonymize from 'ip-anonymize'; import { APPROVED_SUBNET_NOT_FOUND, UNAUTHORIZED_RESOURCE, } from '../../errors/errors.constants'; -import { Expose } from '../../providers/prisma/prisma.interface'; import { GeolocationService } from '../../providers/geolocation/geolocation.service'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { PrismaService } from '../../providers/prisma/prisma.service'; @Injectable() @@ -29,53 +29,53 @@ export class ApprovedSubnetsService { params: { skip?: number; take?: number; - cursor?: Prisma.approvedSubnetsWhereUniqueInput; - where?: Prisma.approvedSubnetsWhereInput; - orderBy?: Prisma.approvedSubnetsOrderByInput; + cursor?: Prisma.ApprovedSubnetWhereUniqueInput; + where?: Prisma.ApprovedSubnetWhereInput; + orderBy?: Prisma.ApprovedSubnetOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const approvedSubnets = await this.prisma.approvedSubnets.findMany({ + const ApprovedSubnet = await this.prisma.approvedSubnet.findMany({ skip, take, cursor, where: { ...where, user: { id: userId } }, orderBy, }); - return approvedSubnets.map((user) => - this.prisma.expose(user), + return ApprovedSubnet.map((user) => + this.prisma.expose(user), ); } async getApprovedSubnet( userId: number, id: number, - ): Promise> { - const approvedSubnet = await this.prisma.approvedSubnets.findUnique({ + ): Promise> { + const ApprovedSubnet = await this.prisma.approvedSubnet.findUnique({ where: { id }, }); - if (!approvedSubnet) throw new NotFoundException(APPROVED_SUBNET_NOT_FOUND); - if (approvedSubnet.userId !== userId) + if (!ApprovedSubnet) throw new NotFoundException(APPROVED_SUBNET_NOT_FOUND); + if (ApprovedSubnet.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - if (!approvedSubnet) throw new NotFoundException(APPROVED_SUBNET_NOT_FOUND); - return this.prisma.expose(approvedSubnet); + if (!ApprovedSubnet) throw new NotFoundException(APPROVED_SUBNET_NOT_FOUND); + return this.prisma.expose(ApprovedSubnet); } async deleteApprovedSubnet( userId: number, id: number, - ): Promise> { - const testApprovedSubnet = await this.prisma.approvedSubnets.findUnique({ + ): Promise> { + const testApprovedSubnet = await this.prisma.approvedSubnet.findUnique({ where: { id }, }); if (!testApprovedSubnet) throw new NotFoundException(APPROVED_SUBNET_NOT_FOUND); if (testApprovedSubnet.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const approvedSubnet = await this.prisma.approvedSubnets.delete({ + const ApprovedSubnet = await this.prisma.approvedSubnet.delete({ where: { id }, }); - return this.prisma.expose(approvedSubnet); + return this.prisma.expose(ApprovedSubnet); } async approveNewSubnet(userId: number, ipAddress: string) { @@ -84,7 +84,7 @@ export class ApprovedSubnetsService { this.configService.get('security.saltRounds') ?? 10, ); const location = await this.geolocationService.getLocation(ipAddress); - const approved = await this.prisma.approvedSubnets.create({ + const approved = await this.prisma.approvedSubnet.create({ data: { user: { connect: { id: userId } }, subnet, @@ -94,7 +94,7 @@ export class ApprovedSubnetsService { countryCode: location?.country?.iso_code, }, }); - return this.prisma.expose(approved); + return this.prisma.expose(approved); } /** @@ -104,14 +104,14 @@ export class ApprovedSubnetsService { async upsertNewSubnet( userId: number, ipAddress: string, - ): Promise> { + ): Promise> { const subnet = anonymize(ipAddress); - const previousSubnets = await this.prisma.approvedSubnets.findMany({ + const previousSubnets = await this.prisma.approvedSubnet.findMany({ where: { user: { id: userId } }, }); for await (const item of previousSubnets) { if (await compare(subnet, item.subnet)) - return this.prisma.expose(item); + return this.prisma.expose(item); } return this.approveNewSubnet(userId, ipAddress); } diff --git a/src/modules/audit-logs/audit-logs.controller.ts b/src/modules/audit-logs/audit-logs.controller.ts index a00a52e03..39498e20b 100644 --- a/src/modules/audit-logs/audit-logs.controller.ts +++ b/src/modules/audit-logs/audit-logs.controller.ts @@ -1,11 +1,11 @@ import { Controller, Get, Param, ParseIntPipe, Query } from '@nestjs/common'; -import { auditLogs } from '@prisma/client'; +import { AuditLog } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; -import { Scopes } from '../auth/scope.decorator'; import { Expose } from '../../providers/prisma/prisma.interface'; +import { Scopes } from '../auth/scope.decorator'; import { AuditLogsService } from './audit-logs.service'; @Controller('groups/:groupId/audit-logs') @@ -21,7 +21,7 @@ export class AuditLogController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.auditLogsService.getAuditLogs(groupId, { skip, take, @@ -36,7 +36,7 @@ export class AuditLogController { async get( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.auditLogsService.getAuditLog(groupId, Number(id)); } } diff --git a/src/modules/audit-logs/audit-logs.service.ts b/src/modules/audit-logs/audit-logs.service.ts index 6bddbabf0..e1a52cb3c 100644 --- a/src/modules/audit-logs/audit-logs.service.ts +++ b/src/modules/audit-logs/audit-logs.service.ts @@ -3,8 +3,8 @@ import { NotFoundException, UnauthorizedException, } from '@nestjs/common'; -import { auditLogs } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { AuditLog } from '@prisma/client'; import { UNAUTHORIZED_RESOURCE } from '../../errors/errors.constants'; import { Expose } from '../../providers/prisma/prisma.interface'; import { PrismaService } from '../../providers/prisma/prisma.service'; @@ -18,29 +18,29 @@ export class AuditLogsService { params: { skip?: number; take?: number; - cursor?: Prisma.auditLogsWhereUniqueInput; - where?: Prisma.auditLogsWhereInput; - orderBy?: Prisma.auditLogsOrderByInput; + cursor?: Prisma.AuditLogWhereUniqueInput; + where?: Prisma.AuditLogWhereInput; + orderBy?: Prisma.AuditLogOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const auditLogs = await this.prisma.auditLogs.findMany({ + const AuditLog = await this.prisma.auditLog.findMany({ skip, take, cursor, where: { ...where, group: { id: groupId } }, orderBy, }); - return auditLogs.map((group) => this.prisma.expose(group)); + return AuditLog.map((group) => this.prisma.expose(group)); } - async getAuditLog(groupId: number, id: number): Promise> { - const auditLog = await this.prisma.auditLogs.findUnique({ + async getAuditLog(groupId: number, id: number): Promise> { + const AuditLog = await this.prisma.auditLog.findUnique({ where: { id }, }); - if (!auditLog) throw new NotFoundException(UNAUTHORIZED_RESOURCE); - if (auditLog.groupId !== groupId) + if (!AuditLog) throw new NotFoundException(UNAUTHORIZED_RESOURCE); + if (AuditLog.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(auditLog); + return this.prisma.expose(AuditLog); } } diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 94b654f6a..b7fa7040f 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -1,5 +1,5 @@ import { Body, Controller, Headers, Ip, Post } from '@nestjs/common'; -import { users } from '@prisma/client'; +import { User } from '@prisma/client'; import { Expose } from '../../providers/prisma/prisma.interface'; import { ForgotPasswordDto, @@ -41,7 +41,7 @@ export class AuthController { async register( @Ip() ip: string, @Body() data: RegisterDto, - ): Promise> { + ): Promise> { return this.authService.register(ip, data); } diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index cd656526b..19996ca83 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -8,8 +8,8 @@ import { } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { Authenticator } from '@otplib/core'; -import { emails, MfaMethod, users } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { Email, MfaMethod, User } from '@prisma/client'; import { compare, hash } from 'bcrypt'; import { createHash } from 'crypto'; import got from 'got/dist/source'; @@ -91,7 +91,7 @@ export class AuthService { code?: string, ): Promise { const emailSafe = safeEmail(email); - const user = await this.prisma.users.findFirst({ + const user = await this.prisma.user.findFirst({ where: { emails: { some: { emailSafe } } }, include: { emails: true, @@ -100,7 +100,7 @@ export class AuthService { }); if (!user) throw new NotFoundException(USER_NOT_FOUND); if (!user.active) - await this.prisma.users.update({ + await this.prisma.user.update({ where: { id: user.id }, data: { active: true }, }); @@ -122,13 +122,10 @@ export class AuthService { return this.loginResponse(ipAddress, userAgent, user); } - async register( - ipAddress: string, - _data: RegisterDto, - ): Promise> { + async register(ipAddress: string, _data: RegisterDto): Promise> { const { email, ...data } = _data; const emailSafe = safeEmail(email); - const testUser = await this.prisma.users.findFirst({ + const testUser = await this.prisma.user.findFirst({ where: { emails: { some: { emailSafe } } }, }); if (testUser) throw new ConflictException(EMAIL_USER_CONFLICT); @@ -175,7 +172,7 @@ export class AuthService { } catch (error) {} } - const user = await this.prisma.users.create({ + const user = await this.prisma.user.create({ data: { ...data, emails: { @@ -185,7 +182,7 @@ export class AuthService { include: { emails: { select: { id: true } } }, }); if (user.emails[0]?.id) - await this.prisma.users.update({ + await this.prisma.user.update({ where: { id: user.id }, data: { prefersEmail: { connect: { id: user.emails[0].id } } }, }); @@ -196,7 +193,7 @@ export class AuthService { async sendEmailVerification(email: string, resend = false) { const emailSafe = safeEmail(email); - const emailDetails = await this.prisma.emails.findFirst({ + const emailDetails = await this.prisma.email.findFirst({ where: { emailSafe }, include: { user: true }, }); @@ -229,12 +226,12 @@ export class AuthService { token: string, ): Promise { if (!token) throw new UnprocessableEntityException(NO_TOKEN_PROVIDED); - const session = await this.prisma.sessions.findFirst({ + const session = await this.prisma.session.findFirst({ where: { token }, include: { user: true }, }); if (!session) throw new NotFoundException(SESSION_NOT_FOUND); - await this.prisma.sessions.updateMany({ + await this.prisma.session.updateMany({ where: { token }, data: { ipAddress, userAgent }, }); @@ -246,12 +243,12 @@ export class AuthService { async logout(token: string): Promise { if (!token) throw new UnprocessableEntityException(NO_TOKEN_PROVIDED); - const session = await this.prisma.sessions.findFirst({ + const session = await this.prisma.session.findFirst({ where: { token }, select: { id: true, user: { select: { id: true } } }, }); if (!session) throw new NotFoundException(SESSION_NOT_FOUND); - await this.prisma.sessions.delete({ + await this.prisma.session.delete({ where: { id: session.id }, }); } @@ -266,7 +263,7 @@ export class AuthService { APPROVE_SUBNET_TOKEN, token, ); - const user = await this.prisma.users.findUnique({ where: { id } }); + const user = await this.prisma.user.findUnique({ where: { id } }); if (!user) throw new NotFoundException(USER_NOT_FOUND); await this.approvedSubnetsService.approveNewSubnet(id, ipAddress); return this.loginResponse(ipAddress, userAgent, user); @@ -278,7 +275,7 @@ export class AuthService { */ async getTotpQrCode(userId: number): Promise { const secret = this.tokensService.generateUuid(); - await this.prisma.users.update({ + await this.prisma.user.update({ where: { id: userId }, data: { twoFactorSecret: secret }, }); @@ -295,8 +292,8 @@ export class AuthService { method: MfaMethod, userId: number, code: string, - ): Promise> { - const user = await this.prisma.users.findUnique({ + ): Promise> { + const user = await this.prisma.user.findUnique({ where: { id: userId }, select: { twoFactorSecret: true, twoFactorMethod: true }, }); @@ -307,11 +304,11 @@ export class AuthService { user.twoFactorSecret = this.tokensService.generateUuid(); if (!this.authenticator.check(code, user.twoFactorSecret)) throw new UnauthorizedException(INVALID_MFA_CODE); - const result = await this.prisma.users.update({ + const result = await this.prisma.user.update({ where: { id: userId }, data: { twoFactorMethod: method, twoFactorSecret: user.twoFactorSecret }, }); - return this.prisma.expose(result); + return this.prisma.expose(result); } async loginWithTotp( @@ -336,7 +333,7 @@ export class AuthService { EMAIL_MFA_TOKEN, token, ); - const user = await this.prisma.users.findUnique({ where: { id } }); + const user = await this.prisma.user.findUnique({ where: { id } }); if (!user) throw new NotFoundException(USER_NOT_FOUND); await this.approvedSubnetsService.upsertNewSubnet(id, ipAddress); return this.loginResponse(ipAddress, userAgent, user); @@ -344,7 +341,7 @@ export class AuthService { async requestPasswordReset(email: string) { const emailSafe = safeEmail(email); - const emailDetails = await this.prisma.emails.findFirst({ + const emailDetails = await this.prisma.email.findFirst({ where: { emailSafe }, include: { user: true }, }); @@ -378,13 +375,13 @@ export class AuthService { PASSWORD_RESET_TOKEN, token, ); - const user = await this.prisma.users.findUnique({ where: { id } }); + const user = await this.prisma.user.findUnique({ where: { id } }); if (!user) throw new NotFoundException(USER_NOT_FOUND); password = await this.hashAndValidatePassword( password, !!ignorePwnedPassword, ); - await this.prisma.users.update({ where: { id }, data: { password } }); + await this.prisma.user.update({ where: { id }, data: { password } }); await this.approvedSubnetsService.upsertNewSubnet(id, ipAddress); return this.loginResponse(ipAddress, userAgent, user); } @@ -398,12 +395,12 @@ export class AuthService { EMAIL_VERIFY_TOKEN, token, ); - const result = await this.prisma.emails.update({ + const result = await this.prisma.email.update({ where: { id }, data: { isVerified: true }, include: { user: true }, }); - const groupsToJoin = await this.prisma.groups.findMany({ + const groupsToJoin = await this.prisma.group.findMany({ where: { autoJoinDomain: true, domains: { @@ -413,7 +410,7 @@ export class AuthService { select: { id: true, name: true }, }); for await (const group of groupsToJoin) { - await this.prisma.memberships.create({ + await this.prisma.membership.create({ data: { user: { connect: { id: result.user.id } }, group: { connect: { id: group.id } }, @@ -445,7 +442,7 @@ export class AuthService { id: number, code: string, ): Promise { - const user = await this.prisma.users.findUnique({ + const user = await this.prisma.user.findUnique({ where: { id }, include: { prefersEmail: true }, }); @@ -454,7 +451,7 @@ export class AuthService { throw new BadRequestException(MFA_NOT_ENABLED); if (this.authenticator.check(code, user.twoFactorSecret)) return this.loginResponse(ipAddress, userAgent, user); - const backupCodes = await this.prisma.backupCodes.findMany({ + const backupCodes = await this.prisma.backupCode.findMany({ where: { user: { id } }, }); let usedBackupCode = false; @@ -464,7 +461,7 @@ export class AuthService { if (backupCode.isUsed) throw new UnauthorizedException(MFA_BACKUP_CODE_USED); usedBackupCode = true; - await this.prisma.backupCodes.update({ + await this.prisma.backupCode.update({ where: { id: backupCode.id }, data: { isUsed: true }, }); @@ -496,7 +493,7 @@ export class AuthService { return this.loginResponse(ipAddress, userAgent, user); } - private async getAccessToken(user: users): Promise { + private async getAccessToken(user: User): Promise { const scopes = await this.getScopes(user); const payload: AccessTokenClaims = { id: user.id, @@ -512,12 +509,12 @@ export class AuthService { private async loginResponse( ipAddress: string, userAgent: string, - user: users, + user: User, ): Promise { const token = this.tokensService.generateUuid(); const ua = new UAParser(userAgent); const location = await this.geolocationService.getLocation(ipAddress); - await this.prisma.sessions.create({ + await this.prisma.session.create({ data: { token, ipAddress, @@ -544,8 +541,8 @@ export class AuthService { } private async mfaResponse( - user: users & { - prefersEmail: emails; + user: User & { + prefersEmail: Email; }, forceMethod?: MfaMethod, ): Promise { @@ -597,7 +594,7 @@ export class AuthService { ): Promise { if (!checkLocationOnLogin) return; const subnet = anonymize(ipAddress); - const previousSubnets = await this.prisma.approvedSubnets.findMany({ + const previousSubnets = await this.prisma.approvedSubnet.findMany({ where: { user: { id } }, }); let isApproved = false; @@ -606,7 +603,7 @@ export class AuthService { if (await compare(subnet, item.subnet)) isApproved = true; } if (!isApproved) { - const user = await this.prisma.users.findUnique({ + const user = await this.prisma.user.findUnique({ where: { id }, select: { name: true, prefersEmail: true }, }); @@ -660,9 +657,9 @@ export class AuthService { ); } - async getScopes(user: users): Promise { + async getScopes(user: User): Promise { const scopes: string[] = [`user-${user.id}:*`]; - const memberships = await this.prisma.memberships.findMany({ + const memberships = await this.prisma.membership.findMany({ where: { user: { id: user.id } }, select: { id: true, role: true, group: { select: { id: true } } }, }); @@ -685,7 +682,7 @@ export class AuthService { } private async recursivelyGetSubgroupIds(groupId: number) { - const subgroups = await this.prisma.groups.findMany({ + const subgroups = await this.prisma.group.findMany({ where: { parent: { id: groupId } }, select: { id: true, @@ -723,10 +720,10 @@ export class AuthService { baseUserId: number, mergeUserId: number, ): Promise<{ success: true }> { - const baseUser = await this.prisma.users.findUnique({ + const baseUser = await this.prisma.user.findUnique({ where: { id: baseUserId }, }); - const mergeUser = await this.prisma.users.findUnique({ + const mergeUser = await this.prisma.user.findUnique({ where: { id: mergeUserId }, }); if (!baseUser || !mergeUser) throw new NotFoundException(USER_NOT_FOUND); @@ -752,34 +749,34 @@ export class AuthService { ].forEach((key) => { if (mergeUser[key] != null) combinedUser[key] = mergeUser[key]; }); - await this.prisma.users.update({ + await this.prisma.user.update({ where: { id: baseUserId }, data: combinedUser, }); for await (const dataType of [ - this.prisma.memberships, - this.prisma.emails, - this.prisma.sessions, - this.prisma.approvedSubnets, - this.prisma.backupCodes, - this.prisma.identities, - this.prisma.auditLogs, - this.prisma.apiKeys, + this.prisma.membership, + this.prisma.email, + this.prisma.session, + this.prisma.approvedSubnet, + this.prisma.backupCode, + this.prisma.identity, + this.prisma.auditLog, + this.prisma.apiKey, ]) { - for await (const item of await (dataType as Prisma.emailsDelegate).findMany( + for await (const item of await (dataType as Prisma.EmailDelegate).findMany( { where: { user: { id: mergeUserId } }, select: { id: true }, }, )) - await (dataType as Prisma.emailsDelegate).update({ + await (dataType as Prisma.EmailDelegate).update({ where: { id: item.id }, data: { user: { connect: { id: baseUserId } } }, }); } - await this.prisma.users.delete({ where: { id: mergeUser.id } }); + await this.prisma.user.delete({ where: { id: mergeUser.id } }); return { success: true }; } } diff --git a/src/modules/domains/domains.controller.ts b/src/modules/domains/domains.controller.ts index 8338fa934..c1f769af9 100644 --- a/src/modules/domains/domains.controller.ts +++ b/src/modules/domains/domains.controller.ts @@ -8,14 +8,14 @@ import { Post, Query, } from '@nestjs/common'; -import { domains } from '@prisma/client'; +import { Domain } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { AuditLog } from '../audit-logs/audit-log.decorator'; import { Scopes } from '../auth/scope.decorator'; -import { Expose } from '../../providers/prisma/prisma.interface'; import { DOMAIN_VERIFICATION_HTML, DOMAIN_VERIFICATION_TXT, @@ -33,7 +33,7 @@ export class DomainController { async create( @Param('groupId', ParseIntPipe) groupId: number, @Body() data: CreateDomainDto, - ): Promise> { + ): Promise> { return this.domainsService.createDomain(groupId, data); } @@ -46,7 +46,7 @@ export class DomainController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.domainsService.getDomains(groupId, { skip, take, @@ -61,7 +61,7 @@ export class DomainController { async get( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.domainsService.getDomain(groupId, Number(id)); } @@ -71,7 +71,7 @@ export class DomainController { async remove( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.domainsService.deleteDomain(groupId, Number(id)); } @@ -81,7 +81,7 @@ export class DomainController { async verifyTxt( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.domainsService.verifyDomain( groupId, Number(id), @@ -95,7 +95,7 @@ export class DomainController { async verifyHtml( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.domainsService.verifyDomain( groupId, Number(id), diff --git a/src/modules/domains/domains.service.ts b/src/modules/domains/domains.service.ts index 62d7fdc22..46a33475e 100644 --- a/src/modules/domains/domains.service.ts +++ b/src/modules/domains/domains.service.ts @@ -5,8 +5,8 @@ import { UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { domains } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { Domain } from '@prisma/client'; import got from 'got'; import { URL } from 'url'; import { @@ -33,8 +33,8 @@ export class DomainsService { async createDomain( groupId: number, - data: Omit, 'verificationCode'>, - ): Promise { + data: Omit, 'verificationCode'>, + ): Promise { try { const fullUrl = new URL(data.domain); data.domain = fullUrl.hostname; @@ -46,7 +46,7 @@ export class DomainsService { ) throw new BadRequestException(INVALID_DOMAIN); const verificationCode = this.tokensService.generateUuid(); - const currentProfilePicture = await this.prisma.groups.findUnique({ + const currentProfilePicture = await this.prisma.group.findUnique({ where: { id: groupId }, select: { profilePictureUrl: true }, }); @@ -59,7 +59,7 @@ export class DomainsService { responseType: 'buffer', }); if (img.body.byteLength > 1) - await this.prisma.groups.update({ + await this.prisma.group.update({ where: { id: groupId }, data: { profilePictureUrl: `https://logo.clearbit.com/${data.domain}`, @@ -67,7 +67,7 @@ export class DomainsService { }); } catch (error) {} - return this.prisma.domains.create({ + return this.prisma.domain.create({ data: { ...data, verificationCode, @@ -81,38 +81,38 @@ export class DomainsService { params: { skip?: number; take?: number; - cursor?: Prisma.domainsWhereUniqueInput; - where?: Prisma.domainsWhereInput; - orderBy?: Prisma.domainsOrderByInput; + cursor?: Prisma.DomainWhereUniqueInput; + where?: Prisma.DomainWhereInput; + orderBy?: Prisma.DomainOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const domains = await this.prisma.domains.findMany({ + const domains = await this.prisma.domain.findMany({ skip, take, cursor, where: { ...where, group: { id: groupId } }, orderBy, }); - return domains.map((group) => this.prisma.expose(group)); + return domains.map((group) => this.prisma.expose(group)); } - async getDomain(groupId: number, id: number): Promise> { - const domain = await this.prisma.domains.findUnique({ + async getDomain(groupId: number, id: number): Promise> { + const domain = await this.prisma.domain.findUnique({ where: { id }, }); if (!domain) throw new NotFoundException(DOMAIN_NOT_FOUND); if (domain.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(domain); + return this.prisma.expose(domain); } async verifyDomain( groupId: number, id: number, method: DomainVerificationMethods, - ): Promise> { - const domain = await this.prisma.domains.findUnique({ + ): Promise> { + const domain = await this.prisma.domain.findUnique({ where: { id }, }); if (!domain) throw new NotFoundException(DOMAIN_NOT_FOUND); @@ -121,7 +121,7 @@ export class DomainsService { if (method === DOMAIN_VERIFICATION_TXT) { const txtRecords = await this.dnsService.lookup(domain.domain, 'TXT'); if (JSON.stringify(txtRecords).includes(domain.verificationCode)) { - await this.prisma.domains.update({ + await this.prisma.domain.update({ where: { id }, data: { isVerified: true }, }); @@ -137,7 +137,7 @@ export class DomainsService { verified = body.includes(domain.verificationCode); } catch (error) {} if (verified) { - await this.prisma.domains.update({ + await this.prisma.domain.update({ where: { id }, data: { isVerified: true }, }); @@ -146,16 +146,16 @@ export class DomainsService { return domain; } - async deleteDomain(groupId: number, id: number): Promise> { - const testDomain = await this.prisma.domains.findUnique({ + async deleteDomain(groupId: number, id: number): Promise> { + const testDomain = await this.prisma.domain.findUnique({ where: { id }, }); if (!testDomain) throw new NotFoundException(DOMAIN_NOT_FOUND); if (testDomain.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const domain = await this.prisma.domains.delete({ + const domain = await this.prisma.domain.delete({ where: { id }, }); - return this.prisma.expose(domain); + return this.prisma.expose(domain); } } diff --git a/src/modules/emails/emails.controller.ts b/src/modules/emails/emails.controller.ts index 9a32139ae..af0204b9e 100644 --- a/src/modules/emails/emails.controller.ts +++ b/src/modules/emails/emails.controller.ts @@ -8,12 +8,12 @@ import { Post, Query, } from '@nestjs/common'; -import { emails } from '@prisma/client'; -import { Expose } from '../../providers/prisma/prisma.interface'; +import { Email } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { Scopes } from '../auth/scope.decorator'; import { CreateEmailDto } from './emails.dto'; import { EmailsService } from './emails.service'; @@ -27,7 +27,7 @@ export class EmailController { async create( @Param('userId', ParseIntPipe) userId: number, @Body() data: CreateEmailDto, - ): Promise> { + ): Promise> { return this.emailsService.createEmail(userId, data); } @@ -40,7 +40,7 @@ export class EmailController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.emailsService.getEmails(userId, { skip, take, @@ -55,7 +55,7 @@ export class EmailController { async get( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.emailsService.getEmail(userId, Number(id)); } @@ -64,7 +64,7 @@ export class EmailController { async remove( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.emailsService.deleteEmail(userId, Number(id)); } } diff --git a/src/modules/emails/emails.service.ts b/src/modules/emails/emails.service.ts index ed4c49a0f..b6e546148 100644 --- a/src/modules/emails/emails.service.ts +++ b/src/modules/emails/emails.service.ts @@ -4,8 +4,8 @@ import { NotFoundException, UnauthorizedException, } from '@nestjs/common'; -import { emails } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { Email } from '@prisma/client'; import { EMAIL_DELETE_PRIMARY, EMAIL_NOT_FOUND, @@ -28,10 +28,10 @@ export class EmailsService { async createEmail( userId: number, - data: Omit, 'user'>, - ): Promise { + data: Omit, 'user'>, + ): Promise { const emailSafe = safeEmail(data.email); - const result = await this.prisma.emails.create({ + const result = await this.prisma.email.create({ data: { ...data, emailSafe, user: { connect: { id: userId } } }, }); await this.auth.sendEmailVerification(data.email); @@ -43,58 +43,58 @@ export class EmailsService { params: { skip?: number; take?: number; - cursor?: Prisma.emailsWhereUniqueInput; - where?: Prisma.emailsWhereInput; - orderBy?: Prisma.emailsOrderByInput; + cursor?: Prisma.EmailWhereUniqueInput; + where?: Prisma.EmailWhereInput; + orderBy?: Prisma.EmailOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const emails = await this.prisma.emails.findMany({ + const emails = await this.prisma.email.findMany({ skip, take, cursor, where: { ...where, user: { id: userId } }, orderBy, }); - return emails.map((user) => this.prisma.expose(user)); + return emails.map((user) => this.prisma.expose(user)); } - async getEmail(userId: number, id: number): Promise> { - const email = await this.prisma.emails.findUnique({ + async getEmail(userId: number, id: number): Promise> { + const email = await this.prisma.email.findUnique({ where: { id }, }); if (!email) throw new NotFoundException(EMAIL_NOT_FOUND); if (email.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(email); + return this.prisma.expose(email); } - async deleteEmail(userId: number, id: number): Promise> { - const testEmail = await this.prisma.emails.findUnique({ + async deleteEmail(userId: number, id: number): Promise> { + const testEmail = await this.prisma.email.findUnique({ where: { id }, }); if (!testEmail) throw new NotFoundException(EMAIL_NOT_FOUND); if (testEmail.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const user = await this.prisma.users.findUnique({ + const user = await this.prisma.user.findUnique({ where: { id: userId }, include: { prefersEmail: true }, }); if (!user) throw new NotFoundException(USER_NOT_FOUND); if (user.prefersEmail.id === id) { const otherEmails = ( - await this.prisma.emails.findMany({ where: { user: { id: userId } } }) + await this.prisma.email.findMany({ where: { user: { id: userId } } }) ).filter((i) => i.id !== id); if (!otherEmails.length) throw new BadRequestException(EMAIL_DELETE_PRIMARY); - await this.prisma.users.update({ + await this.prisma.user.update({ where: { id: userId }, data: { prefersEmail: { connect: { id: otherEmails[0].id } } }, }); } - const email = await this.prisma.emails.delete({ + const email = await this.prisma.email.delete({ where: { id }, }); - return this.prisma.expose(email); + return this.prisma.expose(email); } } diff --git a/src/modules/groups/groups.controller.ts b/src/modules/groups/groups.controller.ts index f2098ded2..c5eb7b5ee 100644 --- a/src/modules/groups/groups.controller.ts +++ b/src/modules/groups/groups.controller.ts @@ -9,7 +9,7 @@ import { Put, Query, } from '@nestjs/common'; -import { groups } from '@prisma/client'; +import { Group } from '@prisma/client'; import { Expose } from '../../providers/prisma/prisma.interface'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; @@ -33,7 +33,7 @@ export class GroupController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.groupsService.getGroups({ skip, take, @@ -49,7 +49,7 @@ export class GroupController { @Param('groupId', ParseIntPipe) id: number, @Query('select', SelectIncludePipe) select?: Record, @Query('include', SelectIncludePipe) include?: Record, - ): Promise> { + ): Promise> { return this.groupsService.getGroup(Number(id), { select, include }); } @@ -59,7 +59,7 @@ export class GroupController { async update( @Body() data: UpdateGroupDto, @Param('groupId', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.groupsService.updateGroup(Number(id), data); } @@ -69,7 +69,7 @@ export class GroupController { async replace( @Body() data: ReplaceGroupDto, @Param('groupId', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.groupsService.updateGroup(Number(id), data); } @@ -78,7 +78,7 @@ export class GroupController { @Scopes('group-{groupId}:delete') async remove( @Param('groupId', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.groupsService.deleteGroup(Number(id)); } @@ -91,7 +91,7 @@ export class GroupController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.groupsService.getSubgroups(id, { skip, take, diff --git a/src/modules/groups/groups.service.ts b/src/modules/groups/groups.service.ts index 4c98836f8..3d890f8d2 100644 --- a/src/modules/groups/groups.service.ts +++ b/src/modules/groups/groups.service.ts @@ -1,6 +1,6 @@ import { Injectable, NotFoundException } from '@nestjs/common'; -import { groups } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { Group } from '@prisma/client'; import randomColor from 'randomcolor'; import { GROUP_NOT_FOUND } from '../../errors/errors.constants'; import { Expose } from '../../providers/prisma/prisma.interface'; @@ -12,7 +12,7 @@ export class GroupsService { async createGroup( userId: number, - data: Omit, 'user'>, + data: Omit, 'user'>, ) { let initials = data.name.trim().substr(0, 2).toUpperCase(); if (data.name.includes(' ')) @@ -26,7 +26,7 @@ export class GroupsService { `https://ui-avatars.com/api/?name=${initials}&background=${randomColor({ luminosity: 'light', })}&color=000000`; - return this.prisma.groups.create({ + return this.prisma.group.create({ include: { memberships: { include: { group: true } } }, data: { ...data, @@ -40,19 +40,19 @@ export class GroupsService { async getGroups(params: { skip?: number; take?: number; - cursor?: Prisma.groupsWhereUniqueInput; - where?: Prisma.groupsWhereInput; - orderBy?: Prisma.groupsOrderByInput; - }): Promise[]> { + cursor?: Prisma.GroupWhereUniqueInput; + where?: Prisma.GroupWhereInput; + orderBy?: Prisma.GroupOrderByInput; + }): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const groups = await this.prisma.groups.findMany({ + const groups = await this.prisma.group.findMany({ skip, take, cursor, where, orderBy, }); - return groups.map((user) => this.prisma.expose(user)); + return groups.map((user) => this.prisma.expose(user)); } async getGroup( @@ -64,55 +64,55 @@ export class GroupsService { select?: Record; include?: Record; }, - ): Promise> { - const group = await this.prisma.groups.findUnique({ + ): Promise> { + const group = await this.prisma.group.findUnique({ where: { id }, select, include, } as any); if (!group) throw new NotFoundException(GROUP_NOT_FOUND); - return this.prisma.expose(group); + return this.prisma.expose(group); } async updateGroup( id: number, - data: Prisma.groupsUpdateInput, - ): Promise> { - const testGroup = await this.prisma.groups.findUnique({ + data: Prisma.GroupUpdateInput, + ): Promise> { + const testGroup = await this.prisma.group.findUnique({ where: { id }, }); if (!testGroup) throw new NotFoundException(GROUP_NOT_FOUND); - const group = await this.prisma.groups.update({ + const group = await this.prisma.group.update({ where: { id }, data, }); - return this.prisma.expose(group); + return this.prisma.expose(group); } async replaceGroup( id: number, - data: Prisma.groupsCreateInput, - ): Promise> { - const testGroup = await this.prisma.groups.findUnique({ + data: Prisma.GroupCreateInput, + ): Promise> { + const testGroup = await this.prisma.group.findUnique({ where: { id }, }); if (!testGroup) throw new NotFoundException(GROUP_NOT_FOUND); - const group = await this.prisma.groups.update({ + const group = await this.prisma.group.update({ where: { id }, data, }); - return this.prisma.expose(group); + return this.prisma.expose(group); } - async deleteGroup(id: number): Promise> { - const testGroup = await this.prisma.groups.findUnique({ + async deleteGroup(id: number): Promise> { + const testGroup = await this.prisma.group.findUnique({ where: { id }, }); if (!testGroup) throw new NotFoundException(GROUP_NOT_FOUND); - const group = await this.prisma.groups.delete({ + const group = await this.prisma.group.delete({ where: { id }, }); - return this.prisma.expose(group); + return this.prisma.expose(group); } async getSubgroups( @@ -120,19 +120,19 @@ export class GroupsService { params: { skip?: number; take?: number; - cursor?: Prisma.groupsWhereUniqueInput; - where?: Prisma.groupsWhereInput; - orderBy?: Prisma.groupsOrderByInput; + cursor?: Prisma.GroupWhereUniqueInput; + where?: Prisma.GroupWhereInput; + orderBy?: Prisma.GroupOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const groups = await this.prisma.groups.findMany({ + const groups = await this.prisma.group.findMany({ skip, take, cursor, where: { ...where, parent: { id } }, orderBy, }); - return groups.map((user) => this.prisma.expose(user)); + return groups.map((user) => this.prisma.expose(user)); } } diff --git a/src/modules/memberships/memberships-group.controller.ts b/src/modules/memberships/memberships-group.controller.ts index c4219d9bc..7eb650eff 100644 --- a/src/modules/memberships/memberships-group.controller.ts +++ b/src/modules/memberships/memberships-group.controller.ts @@ -10,12 +10,12 @@ import { Post, Query, } from '@nestjs/common'; -import { memberships } from '@prisma/client'; -import { Expose } from '../../providers/prisma/prisma.interface'; +import { Membership } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { AuditLog } from '../audit-logs/audit-log.decorator'; import { Scopes } from '../auth/scope.decorator'; import { @@ -35,7 +35,7 @@ export class GroupMembershipController { @Ip() ip: string, @Param('groupId', ParseIntPipe) groupId: number, @Body() data: CreateGroupMembershipDto, - ): Promise> { + ): Promise> { return this.membershipsService.createGroupMembership(ip, groupId, data); } @@ -48,7 +48,7 @@ export class GroupMembershipController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.membershipsService.getMemberships({ skip, take, @@ -63,7 +63,7 @@ export class GroupMembershipController { async get( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.membershipsService.getGroupMembership(groupId, Number(id)); } @@ -74,7 +74,7 @@ export class GroupMembershipController { @Body() data: UpdateMembershipDto, @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.membershipsService.updateGroupMembership( groupId, Number(id), @@ -88,7 +88,7 @@ export class GroupMembershipController { async remove( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.membershipsService.deleteGroupMembership(groupId, Number(id)); } } diff --git a/src/modules/memberships/memberships-user.controller.ts b/src/modules/memberships/memberships-user.controller.ts index 280a3cd81..8538f3430 100644 --- a/src/modules/memberships/memberships-user.controller.ts +++ b/src/modules/memberships/memberships-user.controller.ts @@ -8,12 +8,12 @@ import { Post, Query, } from '@nestjs/common'; -import { memberships } from '@prisma/client'; -import { Expose } from '../../providers/prisma/prisma.interface'; +import { Membership } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { Scopes } from '../auth/scope.decorator'; import { CreateGroupDto } from '../groups/groups.dto'; import { MembershipsService } from './memberships.service'; @@ -27,7 +27,7 @@ export class UserMembershipController { async create( @Param('userId', ParseIntPipe) userId: number, @Body() data: CreateGroupDto, - ): Promise> { + ): Promise> { return this.membershipsService.createUserMembership(userId, data); } @@ -40,7 +40,7 @@ export class UserMembershipController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.membershipsService.getMemberships({ skip, take, @@ -55,7 +55,7 @@ export class UserMembershipController { async get( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.membershipsService.getUserMembership(userId, Number(id)); } @@ -64,7 +64,7 @@ export class UserMembershipController { async remove( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.membershipsService.deleteUserMembership(userId, Number(id)); } } diff --git a/src/modules/memberships/memberships.service.ts b/src/modules/memberships/memberships.service.ts index 03c095fd1..50cafac79 100644 --- a/src/modules/memberships/memberships.service.ts +++ b/src/modules/memberships/memberships.service.ts @@ -5,7 +5,7 @@ import { UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { memberships, users } from '@prisma/client'; +import { Membership, User } from '@prisma/client'; import type { Prisma } from '@prisma/client'; import { CANNOT_DELETE_SOLE_MEMBER, @@ -37,12 +37,12 @@ export class MembershipsService { async getMemberships(params: { skip?: number; take?: number; - cursor?: Prisma.membershipsWhereUniqueInput; - where?: Prisma.membershipsWhereInput; - orderBy?: Prisma.membershipsOrderByInput; - }): Promise[]> { + cursor?: Prisma.MembershipWhereUniqueInput; + where?: Prisma.MembershipWhereInput; + orderBy?: Prisma.MembershipOrderByInput; + }): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const memberships = await this.prisma.memberships.findMany({ + const memberships = await this.prisma.membership.findMany({ skip, take, cursor, @@ -50,61 +50,61 @@ export class MembershipsService { orderBy, include: { group: true, user: true }, }); - return memberships.map((user) => this.prisma.expose(user)); + return memberships.map((user) => this.prisma.expose(user)); } async getUserMembership( userId: number, id: number, - ): Promise> { - const membership = await this.prisma.memberships.findUnique({ + ): Promise> { + const membership = await this.prisma.membership.findUnique({ where: { id }, include: { group: true }, }); if (!membership) throw new NotFoundException(MEMBERSHIP_NOT_FOUND); if (membership.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(membership); + return this.prisma.expose(membership); } async getGroupMembership( groupId: number, id: number, - ): Promise> { - const membership = await this.prisma.memberships.findUnique({ + ): Promise> { + const membership = await this.prisma.membership.findUnique({ where: { id }, include: { user: true }, }); if (!membership) throw new NotFoundException(MEMBERSHIP_NOT_FOUND); if (membership.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(membership); + return this.prisma.expose(membership); } async deleteUserMembership( userId: number, id: number, - ): Promise> { - const testMembership = await this.prisma.memberships.findUnique({ + ): Promise> { + const testMembership = await this.prisma.membership.findUnique({ where: { id }, }); if (!testMembership) throw new NotFoundException(MEMBERSHIP_NOT_FOUND); if (testMembership.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); await this.verifyDeleteMembership(testMembership.groupId, id); - const membership = await this.prisma.memberships.delete({ + const membership = await this.prisma.membership.delete({ where: { id }, }); await this.apiKeyService.removeUnauthorizedScopesForUser(userId); - return this.prisma.expose(membership); + return this.prisma.expose(membership); } async updateGroupMembership( groupId: number, id: number, - data: Prisma.membershipsUpdateInput, - ): Promise> { - const testMembership = await this.prisma.memberships.findUnique({ + data: Prisma.MembershipUpdateInput, + ): Promise> { + const testMembership = await this.prisma.membership.findUnique({ where: { id }, }); if (!testMembership) throw new NotFoundException(MEMBERSHIP_NOT_FOUND); @@ -112,14 +112,14 @@ export class MembershipsService { throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); if (testMembership.role === 'OWNER' && data.role !== 'OWNER') { const otherOwners = ( - await this.prisma.memberships.findMany({ + await this.prisma.membership.findMany({ where: { group: { id: groupId }, role: 'OWNER' }, }) ).filter((i) => i.id !== id); if (!otherOwners.length) throw new BadRequestException(CANNOT_UPDATE_ROLE_SOLE_OWNER); } - const membership = await this.prisma.memberships.update({ + const membership = await this.prisma.membership.update({ where: { id }, data, include: { user: true }, @@ -127,30 +127,30 @@ export class MembershipsService { await this.apiKeyService.removeUnauthorizedScopesForUser( testMembership.userId, ); - return this.prisma.expose(membership); + return this.prisma.expose(membership); } async deleteGroupMembership( groupId: number, id: number, - ): Promise> { - const testMembership = await this.prisma.memberships.findUnique({ + ): Promise> { + const testMembership = await this.prisma.membership.findUnique({ where: { id }, }); if (!testMembership) throw new NotFoundException(MEMBERSHIP_NOT_FOUND); if (testMembership.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); await this.verifyDeleteMembership(testMembership.groupId, id); - const membership = await this.prisma.memberships.delete({ + const membership = await this.prisma.membership.delete({ where: { id }, include: { user: true }, }); await this.apiKeyService.removeUnauthorizedScopesForUser( testMembership.userId, ); - return this.prisma.expose(membership); + return this.prisma.expose(membership); } - async createUserMembership(userId: number, data: Prisma.groupsCreateInput) { + async createUserMembership(userId: number, data: Prisma.GroupCreateInput) { const created = await this.groupsService.createGroup(userId, data); return created.memberships[0]; } @@ -161,15 +161,15 @@ export class MembershipsService { data: CreateMembershipInput, ) { const emailSafe = safeEmail(data.email); - const userResult = await this.prisma.users.findFirst({ + const userResult = await this.prisma.user.findFirst({ where: { emails: { some: { emailSafe } } }, }); - let user: Expose | null = userResult - ? this.prisma.expose(userResult) + let user: Expose | null = userResult + ? this.prisma.expose(userResult) : null; if (!user) user = await this.auth.register(ipAddress, { name: data.email, ...data }); - const result = await this.prisma.memberships.create({ + const result = await this.prisma.membership.create({ data: { role: data.role, group: { connect: { id: groupId } }, @@ -188,7 +188,7 @@ export class MembershipsService { )}/groups/${groupId}`, }, }); - return this.prisma.expose(result); + return this.prisma.expose(result); } /** Verify whether a group membership can be deleted */ @@ -196,12 +196,12 @@ export class MembershipsService { groupId: number, membershipId: number, ): Promise { - const memberships = await this.prisma.memberships.findMany({ + const memberships = await this.prisma.membership.findMany({ where: { group: { id: groupId } }, }); if (memberships.length === 1) throw new BadRequestException(CANNOT_DELETE_SOLE_MEMBER); - const membership = await this.prisma.memberships.findUnique({ + const membership = await this.prisma.membership.findUnique({ where: { id: membershipId }, }); if (!membership) throw new NotFoundException(MEMBERSHIP_NOT_FOUND); diff --git a/src/modules/multi-factor-authentication/multi-factor-authentication.controller.ts b/src/modules/multi-factor-authentication/multi-factor-authentication.controller.ts index 816949873..4ec21341f 100644 --- a/src/modules/multi-factor-authentication/multi-factor-authentication.controller.ts +++ b/src/modules/multi-factor-authentication/multi-factor-authentication.controller.ts @@ -7,7 +7,7 @@ import { ParseIntPipe, Post, } from '@nestjs/common'; -import { users } from '@prisma/client'; +import { User } from '@prisma/client'; import { MFA_PHONE_OR_TOKEN_REQUIRED } from '../../errors/errors.constants'; import { Expose } from '../../providers/prisma/prisma.interface'; import { Scopes } from '../auth/scope.decorator'; @@ -35,7 +35,7 @@ export class MultiFactorAuthenticationController { @Scopes('user-{userId}:delete-mfa-*') async disable2FA( @Param('userId', ParseIntPipe) userId: number, - ): Promise> { + ): Promise> { return this.multiFactorAuthenticationService.disableMfa(userId); } diff --git a/src/modules/multi-factor-authentication/multi-factor-authentication.service.ts b/src/modules/multi-factor-authentication/multi-factor-authentication.service.ts index 9f081961f..b2f12abc2 100644 --- a/src/modules/multi-factor-authentication/multi-factor-authentication.service.ts +++ b/src/modules/multi-factor-authentication/multi-factor-authentication.service.ts @@ -5,8 +5,8 @@ import { NotFoundException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { users } from '@prisma/client'; import type { MfaMethod } from '@prisma/client'; +import { User } from '@prisma/client'; import { hash } from 'bcrypt'; import { MFA_ENABLED_CONFLICT, @@ -14,12 +14,12 @@ import { NO_EMAILS, USER_NOT_FOUND, } from '../../errors/errors.constants'; -import { AuthService } from '../auth/auth.service'; import { MailService } from '../../providers/mail/mail.service'; import { Expose } from '../../providers/prisma/prisma.interface'; import { PrismaService } from '../../providers/prisma/prisma.service'; import { TokensService } from '../../providers/tokens/tokens.service'; import { TwilioService } from '../../providers/twilio/twilio.service'; +import { AuthService } from '../auth/auth.service'; @Injectable() export class MultiFactorAuthenticationService { @@ -33,7 +33,7 @@ export class MultiFactorAuthenticationService { ) {} async requestTotpMfa(userId: number): Promise { - const enabled = await this.prisma.users.findUnique({ + const enabled = await this.prisma.user.findUnique({ where: { id: userId }, select: { twoFactorMethod: true }, }); @@ -44,7 +44,7 @@ export class MultiFactorAuthenticationService { } async requestSmsMfa(userId: number, phone: string): Promise { - const enabled = await this.prisma.users.findUnique({ + const enabled = await this.prisma.user.findUnique({ where: { id: userId }, select: { twoFactorMethod: true }, }); @@ -52,7 +52,7 @@ export class MultiFactorAuthenticationService { if (enabled.twoFactorMethod !== 'NONE') throw new ConflictException(MFA_ENABLED_CONFLICT); const secret = this.tokensService.generateUuid(); - await this.prisma.users.update({ + await this.prisma.user.update({ where: { id: userId }, data: { twoFactorSecret: secret, twoFactorPhone: phone }, }); @@ -65,7 +65,7 @@ export class MultiFactorAuthenticationService { } async requestEmailMfa(userId: number): Promise { - const user = await this.prisma.users.findUnique({ + const user = await this.prisma.user.findUnique({ where: { id: userId }, select: { twoFactorMethod: true, @@ -78,7 +78,7 @@ export class MultiFactorAuthenticationService { if (user.twoFactorMethod !== 'NONE') throw new ConflictException(MFA_ENABLED_CONFLICT); const secret = this.tokensService.generateUuid(); - await this.prisma.users.update({ + await this.prisma.user.update({ where: { id: userId }, data: { twoFactorSecret: secret }, }); @@ -102,23 +102,23 @@ export class MultiFactorAuthenticationService { return this.regenerateBackupCodes(userId); } - async disableMfa(userId: number): Promise> { - const enabled = await this.prisma.users.findUnique({ + async disableMfa(userId: number): Promise> { + const enabled = await this.prisma.user.findUnique({ where: { id: userId }, select: { twoFactorMethod: true }, }); if (!enabled) throw new NotFoundException(USER_NOT_FOUND); if (enabled.twoFactorMethod === 'NONE') throw new BadRequestException(MFA_NOT_ENABLED); - const user = await this.prisma.users.update({ + const user = await this.prisma.user.update({ where: { id: userId }, data: { twoFactorMethod: 'NONE', twoFactorSecret: null }, }); - return this.prisma.expose(user); + return this.prisma.expose(user); } async regenerateBackupCodes(id: number) { - await this.prisma.backupCodes.deleteMany({ where: { user: { id } } }); + await this.prisma.backupCode.deleteMany({ where: { user: { id } } }); const codes: string[] = []; for await (const _ of [...Array(10)]) { const unsafeCode = this.tokensService.generateUuid(); @@ -127,7 +127,7 @@ export class MultiFactorAuthenticationService { unsafeCode, this.configService.get('security.saltRounds') ?? 10, ); - await this.prisma.backupCodes.create({ + await this.prisma.backupCode.create({ data: { user: { connect: { id } }, code }, }); } diff --git a/src/modules/sessions/sessions.controller.ts b/src/modules/sessions/sessions.controller.ts index 9db217857..f8b08e272 100644 --- a/src/modules/sessions/sessions.controller.ts +++ b/src/modules/sessions/sessions.controller.ts @@ -6,12 +6,12 @@ import { ParseIntPipe, Query, } from '@nestjs/common'; -import { sessions } from '@prisma/client'; -import { Expose } from '../../providers/prisma/prisma.interface'; +import { Session } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { Scopes } from '../auth/scope.decorator'; import { SessionsService } from './sessions.service'; @@ -28,7 +28,7 @@ export class SessionController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.sessionsService.getSessions(userId, { skip, take, @@ -43,7 +43,7 @@ export class SessionController { async get( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.sessionsService.getSession(userId, Number(id)); } @@ -52,7 +52,7 @@ export class SessionController { async remove( @Param('userId', ParseIntPipe) userId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.sessionsService.deleteSession(userId, Number(id)); } } diff --git a/src/modules/sessions/sessions.service.ts b/src/modules/sessions/sessions.service.ts index dfa15f457..416c04cad 100644 --- a/src/modules/sessions/sessions.service.ts +++ b/src/modules/sessions/sessions.service.ts @@ -3,8 +3,8 @@ import { NotFoundException, UnauthorizedException, } from '@nestjs/common'; -import { sessions } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { Session } from '@prisma/client'; import { SESSION_NOT_FOUND, UNAUTHORIZED_RESOURCE, @@ -20,43 +20,43 @@ export class SessionsService { params: { skip?: number; take?: number; - cursor?: Prisma.sessionsWhereUniqueInput; - where?: Prisma.sessionsWhereInput; - orderBy?: Prisma.sessionsOrderByInput; + cursor?: Prisma.SessionWhereUniqueInput; + where?: Prisma.SessionWhereInput; + orderBy?: Prisma.SessionOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const sessions = await this.prisma.sessions.findMany({ + const sessions = await this.prisma.session.findMany({ skip, take, cursor, where: { ...where, user: { id: userId } }, orderBy, }); - return sessions.map((user) => this.prisma.expose(user)); + return sessions.map((user) => this.prisma.expose(user)); } - async getSession(userId: number, id: number): Promise> { - const session = await this.prisma.sessions.findUnique({ + async getSession(userId: number, id: number): Promise> { + const session = await this.prisma.session.findUnique({ where: { id }, }); if (!session) throw new NotFoundException(SESSION_NOT_FOUND); if (session.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); if (!session) throw new NotFoundException(SESSION_NOT_FOUND); - return this.prisma.expose(session); + return this.prisma.expose(session); } - async deleteSession(userId: number, id: number): Promise> { - const testSession = await this.prisma.sessions.findUnique({ + async deleteSession(userId: number, id: number): Promise> { + const testSession = await this.prisma.session.findUnique({ where: { id }, }); if (!testSession) throw new NotFoundException(SESSION_NOT_FOUND); if (testSession.userId !== userId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const session = await this.prisma.sessions.delete({ + const session = await this.prisma.session.delete({ where: { id }, }); - return this.prisma.expose(session); + return this.prisma.expose(session); } } diff --git a/src/modules/stripe/stripe.service.ts b/src/modules/stripe/stripe.service.ts index 0399f5014..c131e2f8e 100644 --- a/src/modules/stripe/stripe.service.ts +++ b/src/modules/stripe/stripe.service.ts @@ -6,6 +6,7 @@ import { NotFoundException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; +import Stripe from 'stripe'; import { BILLING_ACCOUNT_CREATED_CONFLICT, BILLING_NOT_FOUND, @@ -15,7 +16,6 @@ import { SOURCE_NOT_FOUND, SUBSCRIPTION_NOT_FOUND, } from '../../errors/errors.constants'; -import Stripe from 'stripe'; import { PrismaService } from '../../providers/prisma/prisma.service'; @Injectable() @@ -36,7 +36,7 @@ export class StripeService { } async createCustomer(groupId: number, data: Stripe.CustomerCreateParams) { - const group = await this.prisma.groups.findUnique({ + const group = await this.prisma.group.findUnique({ where: { id: groupId }, select: { attributes: true }, }); @@ -45,7 +45,7 @@ export class StripeService { if (attributes?.stripeCustomerId) throw new ConflictException(BILLING_ACCOUNT_CREATED_CONFLICT); const result = await this.stripe.customers.create(data); - await this.prisma.groups.update({ + await this.prisma.group.update({ where: { id: groupId }, data: { attributes: { stripeCustomerId: result.id } }, }); @@ -70,7 +70,7 @@ export class StripeService { const result = (await this.stripe.customers.del( stripeId, )) as Stripe.DeletedCustomer; - await this.prisma.groups.update({ + await this.prisma.group.update({ where: { id: groupId }, data: { attributes: { stripeCustomerId: null } }, }); @@ -254,7 +254,7 @@ export class StripeService { /** Get the Stripe customer ID from a group or throw an error */ private async stripeId(groupId: number): Promise { - const group = await this.prisma.groups.findUnique({ + const group = await this.prisma.group.findUnique({ where: { id: groupId }, select: { attributes: true }, }); diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index f4718e6b6..d1b1d9fef 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -9,7 +9,7 @@ import { Post, Query, } from '@nestjs/common'; -import { users } from '@prisma/client'; +import { User } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; @@ -32,13 +32,13 @@ export class UserController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.usersService.getUsers({ skip, take, orderBy, cursor, where }); } @Get(':userId') @Scopes('user-{userId}:read-info') - async get(@Param('userId', ParseIntPipe) id: number): Promise> { + async get(@Param('userId', ParseIntPipe) id: number): Promise> { return this.usersService.getUser(Number(id)); } @@ -47,7 +47,7 @@ export class UserController { async update( @Param('userId', ParseIntPipe) id: number, @Body() data: UpdateUserDto, - ): Promise> { + ): Promise> { return this.usersService.updateUser(Number(id), data); } @@ -55,7 +55,7 @@ export class UserController { @Scopes('user-{userId}:deactivate') async remove( @Param('userId', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.usersService.deactivateUser(Number(id)); } diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 23ae0f5fe..ebeadde77 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -4,8 +4,8 @@ import { NotFoundException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { users } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { User } from '@prisma/client'; import { compare } from 'bcrypt'; import { CURRENT_PASSWORD_REQUIRED, @@ -31,48 +31,48 @@ export class UsersService { private tokensService: TokensService, ) {} - async getUser(id: number): Promise> { - const user = await this.prisma.users.findUnique({ + async getUser(id: number): Promise> { + const user = await this.prisma.user.findUnique({ where: { id }, }); if (!user) throw new NotFoundException(USER_NOT_FOUND); - return this.prisma.expose(user); + return this.prisma.expose(user); } async getUsers(params: { skip?: number; take?: number; - cursor?: Prisma.usersWhereUniqueInput; - where?: Prisma.usersWhereInput; - orderBy?: Prisma.usersOrderByInput; - }): Promise[]> { + cursor?: Prisma.UserWhereUniqueInput; + where?: Prisma.UserWhereInput; + orderBy?: Prisma.UserOrderByInput; + }): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const users = await this.prisma.users.findMany({ + const users = await this.prisma.user.findMany({ skip, take, cursor, where, orderBy, }); - return users.map((user) => this.prisma.expose(user)); + return users.map((user) => this.prisma.expose(user)); } - async createUser(data: Prisma.usersCreateInput): Promise { - return this.prisma.users.create({ + async createUser(data: Prisma.UserCreateInput): Promise { + return this.prisma.user.create({ data, }); } async updateUser( id: number, - data: Omit & PasswordUpdateInput, - ): Promise> { - const transformed: Prisma.usersUpdateInput & PasswordUpdateInput = data; + data: Omit & PasswordUpdateInput, + ): Promise> { + const transformed: Prisma.UserUpdateInput & PasswordUpdateInput = data; if (data.newPassword) { if (!data.currentPassword) throw new BadRequestException(CURRENT_PASSWORD_REQUIRED); const previousPassword = ( - await this.prisma.users.findUnique({ + await this.prisma.user.findUnique({ where: { id }, select: { password: true }, }) @@ -88,26 +88,26 @@ export class UsersService { delete transformed.currentPassword; delete transformed.newPassword; delete transformed.ignorePwnedPassword; - const updateData: Prisma.usersUpdateInput = transformed; - const user = await this.prisma.users.update({ + const updateData: Prisma.UserUpdateInput = transformed; + const user = await this.prisma.user.update({ data: updateData, where: { id }, }); - return this.prisma.expose(user); + return this.prisma.expose(user); } - async deactivateUser(id: number): Promise> { - const user = await this.prisma.users.update({ + async deactivateUser(id: number): Promise> { + const user = await this.prisma.user.update({ where: { id }, data: { active: false }, }); - await this.prisma.sessions.deleteMany({ where: { user: { id } } }); - return this.prisma.expose(user); + await this.prisma.session.deleteMany({ where: { user: { id } } }); + return this.prisma.expose(user); } async requestMerge(userId: number, email: string): Promise<{ queued: true }> { const emailSafe = safeEmail(email); - const user = await this.prisma.users.findFirst({ + const user = await this.prisma.user.findFirst({ where: { emails: { some: { emailSafe } } }, include: { prefersEmail: true }, }); diff --git a/src/modules/webhooks/webhooks.controller.ts b/src/modules/webhooks/webhooks.controller.ts index 3bad42f51..1a2e5633b 100644 --- a/src/modules/webhooks/webhooks.controller.ts +++ b/src/modules/webhooks/webhooks.controller.ts @@ -10,14 +10,14 @@ import { Put, Query, } from '@nestjs/common'; -import { webhooks } from '@prisma/client'; +import { Webhook } from '@prisma/client'; import { CursorPipe } from '../../pipes/cursor.pipe'; import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; import { OrderByPipe } from '../../pipes/order-by.pipe'; import { WherePipe } from '../../pipes/where.pipe'; +import { Expose } from '../../providers/prisma/prisma.interface'; import { AuditLog } from '../audit-logs/audit-log.decorator'; import { Scopes } from '../auth/scope.decorator'; -import { Expose } from '../../providers/prisma/prisma.interface'; import { CreateWebhookDto, ReplaceWebhookDto, @@ -35,7 +35,7 @@ export class WebhookController { async create( @Param('groupId', ParseIntPipe) groupId: number, @Body() data: CreateWebhookDto, - ): Promise> { + ): Promise> { return this.webhooksService.createWebhook(groupId, data); } @@ -48,7 +48,7 @@ export class WebhookController { @Query('cursor', CursorPipe) cursor?: Record, @Query('where', WherePipe) where?: Record, @Query('orderBy', OrderByPipe) orderBy?: Record, - ): Promise[]> { + ): Promise[]> { return this.webhooksService.getWebhooks(groupId, { skip, take, @@ -69,7 +69,7 @@ export class WebhookController { async get( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.webhooksService.getWebhook(groupId, Number(id)); } @@ -80,7 +80,7 @@ export class WebhookController { @Body() data: UpdateWebhookDto, @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.webhooksService.updateWebhook(groupId, Number(id), data); } @@ -91,7 +91,7 @@ export class WebhookController { @Body() data: ReplaceWebhookDto, @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.webhooksService.updateWebhook(groupId, Number(id), data); } @@ -101,7 +101,7 @@ export class WebhookController { async remove( @Param('groupId', ParseIntPipe) groupId: number, @Param('id', ParseIntPipe) id: number, - ): Promise> { + ): Promise> { return this.webhooksService.deleteWebhook(groupId, Number(id)); } } diff --git a/src/modules/webhooks/webhooks.service.ts b/src/modules/webhooks/webhooks.service.ts index d05025fdb..e14d15b87 100644 --- a/src/modules/webhooks/webhooks.service.ts +++ b/src/modules/webhooks/webhooks.service.ts @@ -5,8 +5,8 @@ import { UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { webhooks } from '@prisma/client'; import type { Prisma } from '@prisma/client'; +import { Webhook } from '@prisma/client'; import got from 'got'; import PQueue from 'p-queue'; import pRetry from 'p-retry'; @@ -29,9 +29,9 @@ export class WebhooksService { async createWebhook( groupId: number, - data: Omit, 'group'>, - ): Promise { - return this.prisma.webhooks.create({ + data: Omit, 'group'>, + ): Promise { + return this.prisma.webhook.create({ data: { ...data, group: { connect: { id: groupId } } }, }); } @@ -41,79 +41,79 @@ export class WebhooksService { params: { skip?: number; take?: number; - cursor?: Prisma.webhooksWhereUniqueInput; - where?: Prisma.webhooksWhereInput; - orderBy?: Prisma.webhooksOrderByInput; + cursor?: Prisma.WebhookWhereUniqueInput; + where?: Prisma.WebhookWhereInput; + orderBy?: Prisma.WebhookOrderByInput; }, - ): Promise[]> { + ): Promise[]> { const { skip, take, cursor, where, orderBy } = params; - const webhooks = await this.prisma.webhooks.findMany({ + const webhooks = await this.prisma.webhook.findMany({ skip, take, cursor, where: { ...where, group: { id: groupId } }, orderBy, }); - return webhooks.map((group) => this.prisma.expose(group)); + return webhooks.map((group) => this.prisma.expose(group)); } - async getWebhook(groupId: number, id: number): Promise> { - const webhook = await this.prisma.webhooks.findUnique({ + async getWebhook(groupId: number, id: number): Promise> { + const webhook = await this.prisma.webhook.findUnique({ where: { id }, }); if (!webhook) throw new NotFoundException(WEBHOOK_NOT_FOUND); if (webhook.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - return this.prisma.expose(webhook); + return this.prisma.expose(webhook); } async updateWebhook( groupId: number, id: number, - data: Prisma.webhooksUpdateInput, - ): Promise> { - const testWebhook = await this.prisma.webhooks.findUnique({ + data: Prisma.WebhookUpdateInput, + ): Promise> { + const testWebhook = await this.prisma.webhook.findUnique({ where: { id }, }); if (!testWebhook) throw new NotFoundException(WEBHOOK_NOT_FOUND); if (testWebhook.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const webhook = await this.prisma.webhooks.update({ + const webhook = await this.prisma.webhook.update({ where: { id }, data, }); - return this.prisma.expose(webhook); + return this.prisma.expose(webhook); } async replaceWebhook( groupId: number, id: number, - data: Prisma.webhooksCreateInput, - ): Promise> { - const testWebhook = await this.prisma.webhooks.findUnique({ + data: Prisma.WebhookCreateInput, + ): Promise> { + const testWebhook = await this.prisma.webhook.findUnique({ where: { id }, }); if (!testWebhook) throw new NotFoundException(WEBHOOK_NOT_FOUND); if (testWebhook.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const webhook = await this.prisma.webhooks.update({ + const webhook = await this.prisma.webhook.update({ where: { id }, data, }); - return this.prisma.expose(webhook); + return this.prisma.expose(webhook); } - async deleteWebhook(groupId: number, id: number): Promise> { - const testWebhook = await this.prisma.webhooks.findUnique({ + async deleteWebhook(groupId: number, id: number): Promise> { + const testWebhook = await this.prisma.webhook.findUnique({ where: { id }, }); if (!testWebhook) throw new NotFoundException(WEBHOOK_NOT_FOUND); if (testWebhook.groupId !== groupId) throw new UnauthorizedException(UNAUTHORIZED_RESOURCE); - const webhook = await this.prisma.webhooks.delete({ + const webhook = await this.prisma.webhook.delete({ where: { id }, }); - return this.prisma.expose(webhook); + return this.prisma.expose(webhook); } async getWebhookScopes(): Promise> { @@ -145,7 +145,7 @@ export class WebhooksService { } triggerWebhook(groupId: number, event: string) { - this.prisma.webhooks + this.prisma.webhook .findMany({ where: { group: { id: groupId }, isActive: true, event }, }) @@ -162,7 +162,7 @@ export class WebhooksService { error.name, ); if (error.retriesLeft === 0) - this.prisma.webhooks + this.prisma.webhook .update({ where: { id: webhook.id }, data: { isActive: false }, @@ -179,11 +179,11 @@ export class WebhooksService { .catch((error) => this.logger.error('Unable to get webhooks', error)); } - private async callWebhook(webhook: webhooks, event: string) { + private async callWebhook(webhook: Webhook, event: string) { if (webhook.contentType === 'application/json') await got(webhook.url, { method: 'POST', json: { event } }); else await got(webhook.url, { method: 'POST', body: event }); - await this.prisma.webhooks.update({ + await this.prisma.webhook.update({ where: { id: webhook.id }, data: { lastFiredAt: new Date() }, }); diff --git a/src/providers/prisma/prisma.service.ts b/src/providers/prisma/prisma.service.ts index cf322acf6..e2818c2fb 100644 --- a/src/providers/prisma/prisma.service.ts +++ b/src/providers/prisma/prisma.service.ts @@ -1,10 +1,10 @@ import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; import { - approvedSubnets, - emails, + ApprovedSubnet, + Email, PrismaClient, - sessions, - users, + Session, + User, } from '@prisma/client'; import { Expose } from './prisma.interface'; @@ -23,11 +23,11 @@ export class PrismaService /** Delete sensitive keys from an object */ expose(item: T): Expose { if (!item) return {} as T; - delete ((item as any) as Partial).password; - delete ((item as any) as Partial).twoFactorSecret; - delete ((item as any) as Partial).token; - delete ((item as any) as Partial).emailSafe; - delete ((item as any) as Partial).subnet; + delete ((item as any) as Partial).password; + delete ((item as any) as Partial).twoFactorSecret; + delete ((item as any) as Partial).token; + delete ((item as any) as Partial).emailSafe; + delete ((item as any) as Partial).subnet; return item; } } diff --git a/src/providers/tasks/tasks.service.ts b/src/providers/tasks/tasks.service.ts index d8d4a0861..d4a62c253 100644 --- a/src/providers/tasks/tasks.service.ts +++ b/src/providers/tasks/tasks.service.ts @@ -21,7 +21,7 @@ export class TasksService { this.configService.get('security.unusedRefreshTokenExpiryDays') ?? 30; now.setDate(now.getDate() - unusedRefreshTokenExpiryDays); - const deleted = await this.prisma.sessions.deleteMany({ + const deleted = await this.prisma.session.deleteMany({ where: { updatedAt: { lte: now } }, }); if (deleted.count) @@ -34,7 +34,7 @@ export class TasksService { const inactiveUserDeleteDays = this.configService.get('security.inactiveUserDeleteDays') ?? 30; now.setDate(now.getDate() - inactiveUserDeleteDays); - const deleted = await this.prisma.users.deleteMany({ + const deleted = await this.prisma.user.deleteMany({ where: { active: false, sessions: { every: { updatedAt: { lte: now } } },