Newer
Older
Args,
Context,
Mutation,
Parent,
Query,
ResolveField,
Resolver,
Subscription,
} from '@nestjs/graphql';
import { PrismaService } from '../prisma/prisma.service';
import { Request } from 'express';
import { NotFoundException, UseGuards } from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard';
import { AgentGuard } from '../auth/agent.guard';
import { GroupGuard } from '../auth/group.guard';
import { TokenCode } from '@prisma/client';
import { AgentFlags, AgentModel } from './models/agent.model';
import { PubSub } from 'graphql-subscriptions';
import { TokenCodeInputModel } from './models/token-code-input.model';
@Resolver(() => TokenCodeModel)
export class TokenCodeResolver {
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
constructor(
private readonly pubSub: PubSub,
private readonly prisma: PrismaService,
) {}
@Subscription(() => TokenCodeModel, {
filter: (payload: TokenCode, variables, { req }: { req: Request }) => {
return req.agent && req.agent.id === payload.agentId;
},
})
onTokenCodeUpdate(@Context('pubsub') pubSub: PubSub) {
return pubSub.asyncIterator('onTokenCodeUpdate');
}
@Mutation(() => TokenCodeModel)
@UseGuards(AuthGuard, AgentGuard)
async invalidateTokenCode(
@Args('tokenCode') tokenCode: string,
@Context() { req }: { req: Request },
) {
return await this.prisma.$transaction(async (prisma) => {
const token = await prisma.tokenCode.findFirst({
where: { id: tokenCode },
});
if (!token) throw new NotFoundException();
const newToken = await prisma.tokenCode.create({
data: {
agent: {
connect: { id: req.agent.id },
},
},
include: { agent: true },
});
const oldToken = await prisma.tokenCode.delete({
where: { id: tokenCode },
include: { agent: true },
});
await this.pubSub.publish('onTokenCodeUpdate', newToken);
await this.pubSub.publish('onTokenCodeUpdate', {
id: oldToken.id,
deleted: true,
});
return newToken;
});
}
@Query(() => [TokenCodeModel])
@UseGuards(AuthGuard)
async getTokenCodes(
@Context() { req }: { req: Request },
): Promise<TokenCodeModel[]> {
if (!req.agent) return [];
if ((req.agent.flags & AgentFlags.ADMIN) === AgentFlags.ADMIN)
return await this.prisma.tokenCode.findMany({
include: {
agent: true,
},
});
else
return await this.prisma.tokenCode.findMany({
where: { agentId: req.agent.id },
include: {
agent: {
select: {
id: true,
bio: true,
name: true,
slug: false,
flags: true,
avatar: true,
},
},
},
});
}
@Mutation(() => TokenCodeModel)
@UseGuards(AuthGuard, GroupGuard)
async redeemTokenCode(
@Args('tokenCode') tokenCode: string,
@Context() { req }: { req: Request },
): Promise<TokenCodeModel> {
return this.prisma.$transaction(async (prisma) => {
const token = await prisma.tokenCode.findFirst({
where: { id: tokenCode },
include: {
agent: {
select: {
id: true,
bio: true,
name: true,
slug: false,
flags: true,
avatar: true,
},
},
},
});
if (!token) throw new NotFoundException();
const deleted = await prisma.tokenCode.delete({
where: { id: tokenCode },
});
const { tokenCodes } = await prisma.agent.update({
where: { id: token.agentId },
data: {
tokenCodes: {
create: { value: 1 },
},
},
include: { tokenCodes: true },
});
const group = await prisma.group.update({
where: { id: req.group.id },
data: {
tokens: { increment: token.value },
},
});
await this.pubSub.publish('onTokenCodeUpdate', {
id: deleted.id,
deleted: true,
});
await this.pubSub.publish('onGroupUpdate', group);
await this.pubSub.publish('onTokenCodeUpdate', tokenCodes[0]);
return token;
});
}
@Mutation(() => TokenCodeModel)
@UseGuards(AuthGuard, AgentGuard)
async updateTokenCode(
@Args('id') id: string,
@Args('data') data: TokenCodeInputModel,
@Context() { req }: { req: Request },
): Promise<TokenCode> {
const count = await this.prisma.tokenCode.count({
where: { id, agentId: req.agent.id },
});
if (count === 0) throw new NotFoundException();
const tokenCode = await this.prisma.tokenCode.update({
where: {
id,
},
data,
include: {
agent: {
select: {
id: true,
bio: true,
name: true,
slug: false,
flags: true,
avatar: true,
},
},
},
});
await this.pubSub.publish('onTokenCodeUpdate', tokenCode);
return tokenCode;
}
/*@ResolveField('agent')
async agent(@Parent() parent: TokenCodeModel): Promise<AgentModel> {
return this.prisma.agent.findFirst({
where: {
tokenCodes: {
some: { id: parent.id },
},
},
select: {
id: true,
bio: true,
name: true,
slug: false,
flags: true,
avatar: true,
},
});
}*/