forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.service.ts
More file actions
59 lines (50 loc) · 1.58 KB
/
Copy pathusers.service.ts
File metadata and controls
59 lines (50 loc) · 1.58 KB
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
import { Injectable, NotFoundException } from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
@Injectable()
export class UsersService {
constructor(private prisma: PrismaService) {}
async addPushToken(userId: string, token: string) {
const user = await this.prisma.user.findUnique({ where: { id: userId } });
if (!user) throw new NotFoundException("User not found");
if (!user.pushTokens.includes(token)) {
await this.prisma.user.update({
where: { id: userId },
data: {
pushTokens: {
push: token,
},
},
});
}
return { success: true };
}
async removePushToken(userId: string, token: string) {
const user = await this.prisma.user.findUnique({ where: { id: userId } });
if (!user) throw new NotFoundException("User not found");
const newTokens = user.pushTokens.filter((t) => t !== token);
await this.prisma.user.update({
where: { id: userId },
data: {
pushTokens: {
set: newTokens,
},
},
});
return { success: true };
}
async updatePreferences(userId: string, pushNotificationsEnabled: boolean) {
await this.prisma.user.update({
where: { id: userId },
data: { pushNotificationsEnabled },
});
return { success: true };
}
async getPreferences(userId: string) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: { pushNotificationsEnabled: true },
});
if (!user) throw new NotFoundException("User not found");
return user;
}
}