-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprofiles.service.ts
84 lines (77 loc) ยท 2.31 KB
/
profiles.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { Profile, Prisma } from '@prisma/client';
import { v4 as uuid } from 'uuid';
import { UpdateProfileDto } from './dto/update-profile.dto';
import { CreateProfileDto } from './dto/create-profile.dto';
import { PrismaService } from '../prisma/prisma.service';
import { UploadService } from '../upload/upload.service';
@Injectable()
export class ProfilesService {
constructor(
private prisma: PrismaService,
private uploadService: UploadService,
) {}
async findProfileByUserUuid(userUuid: string): Promise<Profile | null> {
const profile = await this.prisma.profile.findUnique({
where: { userUuid },
});
if (!profile) throw new NotFoundException();
return profile;
}
async findProfileByProfileUuid(uuid: string): Promise<Profile | null> {
return this.prisma.profile.findUnique({ where: { uuid } });
}
async findProfiles(profileUuids: string[]): Promise<Profile[]> {
return this.prisma.profile.findMany({
where: { uuid: { in: profileUuids } },
});
}
async getOrCreateProfile(data: CreateProfileDto): Promise<Profile> {
return this.prisma.profile.upsert({
where: { userUuid: data.userUuid },
update: {},
create: {
uuid: uuid(),
userUuid: data.userUuid,
image: data.image,
nickname: data.nickname,
},
});
}
async updateProfile(
userUuid: string,
profileUuid: string,
image: Express.Multer.File,
updateProfileDto: UpdateProfileDto,
): Promise<Profile | null> {
await this.verifyUserProfile(userUuid, profileUuid);
if (image) {
updateProfileDto.image = await this.uploadService.uploadFile(image);
}
try {
return await this.prisma.profile.update({
where: { userUuid },
data: updateProfileDto,
});
} catch (err) {
if (err instanceof Prisma.PrismaClientKnownRequestError) {
return null;
} else {
throw err;
}
}
}
async verifyUserProfile(
userUuid: string,
profileUuid: string,
): Promise<boolean> {
const profile = await this.findProfileByProfileUuid(profileUuid);
if (!profile) throw new ForbiddenException();
if (userUuid !== profile.userUuid) throw new ForbiddenException();
return true;
}
}