1
- import PairModel , { Pair } from "./models/pair " ;
2
- import UserModel , { User } from "./models/user" ;
3
- import CertificateModel , { Certificate } from "./models/certificate" ;
1
+ import { Attestation , PrismaClient , Tie , User } from "@prisma/client " ;
2
+
3
+ const client = new PrismaClient ( ) ;
4
4
5
5
export async function getUserByDiscordId ( discordId : string ) {
6
- return await UserModel . findOne ( { discordId } ) ;
6
+ return await client . user . findFirst ( { where : { discordId } } ) ;
7
7
}
8
8
9
9
export async function createOrUpdateUser ( {
@@ -13,18 +13,16 @@ export async function createOrUpdateUser({
13
13
discordId : string ;
14
14
youtubeChannelId : string ;
15
15
} ) {
16
- const user = await getUserByDiscordId ( discordId ) ;
17
- if ( user ) {
18
- user . youtubeChannelId = youtubeChannelId ;
19
- return await user . save ( ) ;
20
- }
21
-
22
- const newUser = await UserModel . create ( {
23
- discordId,
24
- youtubeChannelId,
16
+ const newOrUpdatedUser = await client . user . upsert ( {
17
+ where : { discordId } ,
18
+ update : { youtubeChannelId } ,
19
+ create : {
20
+ discordId,
21
+ youtubeChannelId,
22
+ } ,
25
23
} ) ;
26
24
27
- return newUser ;
25
+ return newOrUpdatedUser ;
28
26
}
29
27
30
28
export async function createPair ( {
@@ -35,13 +33,15 @@ export async function createPair({
35
33
guildId : string ;
36
34
roleId : string ;
37
35
originChannelId : string ;
38
- } ) : Promise < Pair > {
39
- const pair = await PairModel . create ( { guildId, roleId, originChannelId } ) ;
36
+ } ) : Promise < Tie > {
37
+ const pair = await client . tie . create ( {
38
+ data : { guildId, roleId, originChannelId } ,
39
+ } ) ;
40
40
return pair ;
41
41
}
42
42
43
- export async function getPairsForGuild ( guildId : string ) : Promise < Pair [ ] > {
44
- const pairs = await PairModel . find ( { guildId } ) ;
43
+ export async function getPairsForGuild ( guildId : string ) : Promise < Tie [ ] > {
44
+ const pairs = await client . tie . findMany ( { where : { guildId } } ) ;
45
45
return pairs ;
46
46
}
47
47
@@ -51,8 +51,10 @@ export async function findCertificate({
51
51
} : {
52
52
user : User ;
53
53
originChannelId : string ;
54
- } ) : Promise < Certificate | null > {
55
- return await CertificateModel . findOne ( { user, originChannelId } ) ;
54
+ } ) : Promise < Attestation | null > {
55
+ return await client . attestation . findFirst ( {
56
+ where : { user, originChannelId } ,
57
+ } ) ;
56
58
}
57
59
58
60
export interface ModifyOptions {
@@ -67,19 +69,28 @@ export async function createOrUpdateCertificate({
67
69
originChannelId,
68
70
valid,
69
71
since,
70
- } : ModifyOptions ) : Promise < Certificate > {
72
+ } : ModifyOptions ) : Promise < Attestation > {
71
73
const cert = await findCertificate ( { user, originChannelId } ) ;
72
74
if ( cert ) {
73
- cert . valid = valid ;
74
- cert . since = since ;
75
- return await cert . save ( ) ;
75
+ const updated = await client . attestation . update ( {
76
+ where : { id : cert . id } ,
77
+ data : {
78
+ valid,
79
+ since : since ?? null ,
80
+ } ,
81
+ } ) ;
82
+ return updated ;
76
83
}
77
84
78
- const newCert = await CertificateModel . create ( {
79
- user,
80
- originChannelId,
81
- valid,
82
- since,
85
+ const newCert = await client . attestation . create ( {
86
+ data : {
87
+ user : {
88
+ connect : user ,
89
+ } ,
90
+ originChannelId,
91
+ valid,
92
+ since,
93
+ } ,
83
94
} ) ;
84
95
85
96
return newCert ;
0 commit comments