-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.ts
More file actions
112 lines (101 loc) · 2.62 KB
/
Copy pathuser.ts
File metadata and controls
112 lines (101 loc) · 2.62 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type { UserDraft } from '../types'
import { eq, sql } from 'drizzle-orm'
import { useDatabase } from '../database'
import { users } from '../tables'
export class User {
static async find(id: string) {
return useDatabase().query.users.findFirst({
where: (users, { eq, and }) => and(
eq(users.id, id),
eq(users.isActive, true),
),
with: {
focusedTask: true,
telegramUsers: true,
},
})
}
static async findByEmail(email: string) {
return useDatabase().query.users.findFirst({
where: (users, { eq, and }) => and(
eq(users.email, email),
eq(users.isActive, true),
),
})
}
static async findByPhone(phone: string) {
return useDatabase().query.users.findFirst({
where: (users, { eq, and }) => and(
eq(users.phone, phone),
eq(users.isActive, true),
),
})
}
static async findStaff() {
return useDatabase().query.users.findMany({
where: (users, { eq, and }) => and(
eq(users.type, 'staff'),
eq(users.isActive, true),
),
orderBy: (users, { asc }) => asc(users.name),
with: {
focusedTask: true,
},
})
}
static async findPartners() {
return useDatabase().query.users.findMany({
where: (users, { eq, and }) => and(
eq(users.type, 'partner'),
eq(users.isActive, true),
),
orderBy: (users, { asc }) => asc(users.name),
with: {
focusedTask: true,
},
})
}
static async findBots() {
return useDatabase().query.users.findMany({
where: (users, { eq }) => eq(users.type, 'bot'),
})
}
static async list() {
return useDatabase().query.users.findMany({
where: (users, { eq }) => eq(users.isActive, true),
orderBy: (users, { asc }) => asc(users.surname),
with: {
focusedTask: true,
telegramUsers: true,
},
})
}
static async create(data: UserDraft) {
const [user] = await useDatabase().insert(users).values(data).returning()
return user
}
static async update(id: string, data: Partial<UserDraft>) {
const [user] = await useDatabase()
.update(users)
.set({
...data,
updatedAt: sql`now()`,
})
.where(eq(users.id, id))
.returning()
return user
}
static async updateOnline(id: string) {
const [user] = await useDatabase()
.update(users)
.set({
onlineAt: sql`now()`,
})
.where(eq(users.id, id))
.returning()
return user
}
static async delete(id: string) {
return useDatabase().delete(users).where(eq(users.id, id))
}
}