Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🐛 Fix Prisma update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 25, 2020
1 parent fd36685 commit d63790e
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 193 deletions.
4 changes: 2 additions & 2 deletions src/interceptors/audit-log.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NestInterceptor,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { auditLogsCreateInput } from '@prisma/client';
import type { Prisma } from '@prisma/client';
import { getClientIp } from 'request-ip';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
Expand Down Expand Up @@ -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: auditLogsCreateInput = {
const data: Prisma.auditLogsCreateInput = {
event,
city: location?.city?.names?.en,
region: location?.subdivisions?.pop()?.names?.en,
Expand Down
58 changes: 25 additions & 33 deletions src/modules/api-keys/api-keys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
apiKeys,
apiKeysCreateInput,
apiKeysOrderByInput,
apiKeysUpdateInput,
apiKeysWhereInput,
apiKeysWhereUniqueInput,
InputJsonValue,
JsonValue,
} from '@prisma/client';
import { apiKeys } from '@prisma/client';
import type { Prisma, InputJsonValue, JsonValue } from '@prisma/client';
import QuickLRU from 'quick-lru';
import {
API_KEY_NOT_FOUND,
Expand Down Expand Up @@ -41,7 +33,7 @@ export class ApiKeysService {

async createApiKeyForGroup(
groupId: number,
data: Omit<Omit<apiKeysCreateInput, 'apiKey'>, 'group'>,
data: Omit<Omit<Prisma.apiKeysCreateInput, 'apiKey'>, 'group'>,
): Promise<apiKeys> {
const apiKey = this.tokensService.generateUuid();
data.scopes = this.cleanScopesForGroup(groupId, data.scopes);
Expand All @@ -51,7 +43,7 @@ export class ApiKeysService {
}
async createApiKeyForUser(
userId: number,
data: Omit<Omit<apiKeysCreateInput, 'apiKey'>, 'user'>,
data: Omit<Omit<Prisma.apiKeysCreateInput, 'apiKey'>, 'user'>,
): Promise<apiKeys> {
const apiKey = this.tokensService.generateUuid();
data.scopes = this.cleanScopesForUser(userId, data.scopes);
Expand All @@ -65,9 +57,9 @@ export class ApiKeysService {
params: {
skip?: number;
take?: number;
cursor?: apiKeysWhereUniqueInput;
where?: apiKeysWhereInput;
orderBy?: apiKeysOrderByInput;
cursor?: Prisma.apiKeysWhereUniqueInput;
where?: Prisma.apiKeysWhereInput;
orderBy?: Prisma.apiKeysOrderByInput;
},
): Promise<Expose<apiKeys>[]> {
const { skip, take, cursor, where, orderBy } = params;
Expand All @@ -85,9 +77,9 @@ export class ApiKeysService {
params: {
skip?: number;
take?: number;
cursor?: apiKeysWhereUniqueInput;
where?: apiKeysWhereInput;
orderBy?: apiKeysOrderByInput;
cursor?: Prisma.apiKeysWhereUniqueInput;
where?: Prisma.apiKeysWhereInput;
orderBy?: Prisma.apiKeysOrderByInput;
},
): Promise<Expose<apiKeys>[]> {
const { skip, take, cursor, where, orderBy } = params;
Expand All @@ -105,7 +97,7 @@ export class ApiKeysService {
groupId: number,
id: number,
): Promise<Expose<apiKeys>> {
const apiKey = await this.prisma.apiKeys.findOne({
const apiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!apiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -114,7 +106,7 @@ export class ApiKeysService {
return this.prisma.expose<apiKeys>(apiKey);
}
async getApiKeyForUser(userId: number, id: number): Promise<Expose<apiKeys>> {
const apiKey = await this.prisma.apiKeys.findOne({
const apiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!apiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -136,9 +128,9 @@ export class ApiKeysService {
async updateApiKeyForGroup(
groupId: number,
id: number,
data: apiKeysUpdateInput,
data: Prisma.apiKeysUpdateInput,
): Promise<Expose<apiKeys>> {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -155,9 +147,9 @@ export class ApiKeysService {
async updateApiKeyForUser(
userId: number,
id: number,
data: apiKeysUpdateInput,
data: Prisma.apiKeysUpdateInput,
): Promise<Expose<apiKeys>> {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -175,9 +167,9 @@ export class ApiKeysService {
async replaceApiKeyForGroup(
groupId: number,
id: number,
data: apiKeysCreateInput,
data: Prisma.apiKeysCreateInput,
): Promise<Expose<apiKeys>> {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -194,9 +186,9 @@ export class ApiKeysService {
async replaceApiKeyForUser(
userId: number,
id: number,
data: apiKeysCreateInput,
data: Prisma.apiKeysCreateInput,
): Promise<Expose<apiKeys>> {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -215,7 +207,7 @@ export class ApiKeysService {
groupId: number,
id: number,
): Promise<Expose<apiKeys>> {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -231,7 +223,7 @@ export class ApiKeysService {
userId: number,
id: number,
): Promise<Expose<apiKeys>> {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -253,7 +245,7 @@ export class ApiKeysService {
where?: { after?: string };
},
) {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand All @@ -270,7 +262,7 @@ export class ApiKeysService {
where?: { after?: string };
},
) {
const testApiKey = await this.prisma.apiKeys.findOne({
const testApiKey = await this.prisma.apiKeys.findUnique({
where: { id },
});
if (!testApiKey) throw new NotFoundException(API_KEY_NOT_FOUND);
Expand Down Expand Up @@ -400,7 +392,7 @@ export class ApiKeysService {
async getApiKeyScopesForGroup(
groupId: number,
): Promise<Record<string, string>> {
const group = await this.prisma.groups.findOne({
const group = await this.prisma.groups.findUnique({
where: { id: groupId },
select: { attributes: true },
});
Expand Down
18 changes: 7 additions & 11 deletions src/modules/approved-subnets/approved-subnets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import {
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
approvedSubnets,
approvedSubnetsOrderByInput,
approvedSubnetsWhereInput,
approvedSubnetsWhereUniqueInput,
} from '@prisma/client';
import { approvedSubnets } from '@prisma/client';
import type { Prisma } from '@prisma/client';
import { compare, hash } from 'bcrypt';
import anonymize from 'ip-anonymize';
import {
Expand All @@ -33,9 +29,9 @@ export class ApprovedSubnetsService {
params: {
skip?: number;
take?: number;
cursor?: approvedSubnetsWhereUniqueInput;
where?: approvedSubnetsWhereInput;
orderBy?: approvedSubnetsOrderByInput;
cursor?: Prisma.approvedSubnetsWhereUniqueInput;
where?: Prisma.approvedSubnetsWhereInput;
orderBy?: Prisma.approvedSubnetsOrderByInput;
},
): Promise<Expose<approvedSubnets>[]> {
const { skip, take, cursor, where, orderBy } = params;
Expand All @@ -55,7 +51,7 @@ export class ApprovedSubnetsService {
userId: number,
id: number,
): Promise<Expose<approvedSubnets>> {
const approvedSubnet = await this.prisma.approvedSubnets.findOne({
const approvedSubnet = await this.prisma.approvedSubnets.findUnique({
where: { id },
});
if (!approvedSubnet) throw new NotFoundException(APPROVED_SUBNET_NOT_FOUND);
Expand All @@ -69,7 +65,7 @@ export class ApprovedSubnetsService {
userId: number,
id: number,
): Promise<Expose<approvedSubnets>> {
const testApprovedSubnet = await this.prisma.approvedSubnets.findOne({
const testApprovedSubnet = await this.prisma.approvedSubnets.findUnique({
where: { id },
});
if (!testApprovedSubnet)
Expand Down
16 changes: 6 additions & 10 deletions src/modules/audit-logs/audit-logs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import {
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import {
auditLogs,
auditLogsOrderByInput,
auditLogsWhereInput,
auditLogsWhereUniqueInput,
} from '@prisma/client';
import { auditLogs } from '@prisma/client';
import type { Prisma } from '@prisma/client';
import { UNAUTHORIZED_RESOURCE } from '../../errors/errors.constants';
import { Expose } from '../../providers/prisma/prisma.interface';
import { PrismaService } from '../../providers/prisma/prisma.service';
Expand All @@ -22,9 +18,9 @@ export class AuditLogsService {
params: {
skip?: number;
take?: number;
cursor?: auditLogsWhereUniqueInput;
where?: auditLogsWhereInput;
orderBy?: auditLogsOrderByInput;
cursor?: Prisma.auditLogsWhereUniqueInput;
where?: Prisma.auditLogsWhereInput;
orderBy?: Prisma.auditLogsOrderByInput;
},
): Promise<Expose<auditLogs>[]> {
const { skip, take, cursor, where, orderBy } = params;
Expand All @@ -39,7 +35,7 @@ export class AuditLogsService {
}

async getAuditLog(groupId: number, id: number): Promise<Expose<auditLogs>> {
const auditLog = await this.prisma.auditLogs.findOne({
const auditLog = await this.prisma.auditLogs.findUnique({
where: { id },
});
if (!auditLog) throw new NotFoundException(UNAUTHORIZED_RESOURCE);
Expand Down
31 changes: 17 additions & 14 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Authenticator } from '@otplib/core';
import { emails, emailsDelegate, MfaMethod, users } from '@prisma/client';
import { emails, MfaMethod, users } from '@prisma/client';
import type { Prisma } from '@prisma/client';
import { compare, hash } from 'bcrypt';
import { createHash } from 'crypto';
import got from 'got/dist/source';
Expand Down Expand Up @@ -265,7 +266,7 @@ export class AuthService {
APPROVE_SUBNET_TOKEN,
token,
);
const user = await this.prisma.users.findOne({ where: { id } });
const user = await this.prisma.users.findUnique({ where: { id } });
if (!user) throw new NotFoundException(USER_NOT_FOUND);
await this.approvedSubnetsService.approveNewSubnet(id, ipAddress);
return this.loginResponse(ipAddress, userAgent, user);
Expand Down Expand Up @@ -295,7 +296,7 @@ export class AuthService {
userId: number,
code: string,
): Promise<Expose<users>> {
const user = await this.prisma.users.findOne({
const user = await this.prisma.users.findUnique({
where: { id: userId },
select: { twoFactorSecret: true, twoFactorMethod: true },
});
Expand Down Expand Up @@ -335,7 +336,7 @@ export class AuthService {
EMAIL_MFA_TOKEN,
token,
);
const user = await this.prisma.users.findOne({ where: { id } });
const user = await this.prisma.users.findUnique({ where: { id } });
if (!user) throw new NotFoundException(USER_NOT_FOUND);
await this.approvedSubnetsService.upsertNewSubnet(id, ipAddress);
return this.loginResponse(ipAddress, userAgent, user);
Expand Down Expand Up @@ -377,7 +378,7 @@ export class AuthService {
PASSWORD_RESET_TOKEN,
token,
);
const user = await this.prisma.users.findOne({ where: { id } });
const user = await this.prisma.users.findUnique({ where: { id } });
if (!user) throw new NotFoundException(USER_NOT_FOUND);
password = await this.hashAndValidatePassword(
password,
Expand Down Expand Up @@ -444,7 +445,7 @@ export class AuthService {
id: number,
code: string,
): Promise<TokenResponse> {
const user = await this.prisma.users.findOne({
const user = await this.prisma.users.findUnique({
where: { id },
include: { prefersEmail: true },
});
Expand Down Expand Up @@ -605,7 +606,7 @@ export class AuthService {
if (await compare(subnet, item.subnet)) isApproved = true;
}
if (!isApproved) {
const user = await this.prisma.users.findOne({
const user = await this.prisma.users.findUnique({
where: { id },
select: { name: true, prefersEmail: true },
});
Expand Down Expand Up @@ -722,10 +723,10 @@ export class AuthService {
baseUserId: number,
mergeUserId: number,
): Promise<{ success: true }> {
const baseUser = await this.prisma.users.findOne({
const baseUser = await this.prisma.users.findUnique({
where: { id: baseUserId },
});
const mergeUser = await this.prisma.users.findOne({
const mergeUser = await this.prisma.users.findUnique({
where: { id: mergeUserId },
});
if (!baseUser || !mergeUser) throw new NotFoundException(USER_NOT_FOUND);
Expand Down Expand Up @@ -766,11 +767,13 @@ export class AuthService {
this.prisma.auditLogs,
this.prisma.apiKeys,
]) {
for await (const item of await (dataType as emailsDelegate).findMany({
where: { user: { id: mergeUserId } },
select: { id: true },
}))
await (dataType as emailsDelegate).update({
for await (const item of await (dataType as Prisma.emailsDelegate).findMany(
{
where: { user: { id: mergeUserId } },
select: { id: true },
},
))
await (dataType as Prisma.emailsDelegate).update({
where: { id: item.id },
data: { user: { connect: { id: baseUserId } } },
});
Expand Down
Loading

0 comments on commit d63790e

Please sign in to comment.