Newer
Older
import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { PrismaService } from '../prisma/prisma.service';
import { AgentModel } from './models/agent.model';
import { Inject, NotFoundException, UseGuards } from '@nestjs/common';
import { AuthGuard } from '../auth/auth.guard';
import { AdminGuard } from '../auth/admin.guard';
import { AgentInputModel } from './models/agent-input.model';
import { GroupModel } from './models/group.model';
import { GroupInputModel } from './models/group-input.model';
import { TokenCodeModel } from './models/token-code.model';
import { TokenCodeInputModel } from './models/token-code-input.model';
import { EntryModel } from './models/entry.model';
import { EntryService } from '../utils/entry.service';
import { PubSub } from 'graphql-subscriptions';
import { GraphQLString } from 'graphql/type';
@Resolver()
export class AdminResolver {
constructor(
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
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
private readonly pubSub: PubSub,
private readonly prisma: PrismaService,
private readonly entryService: EntryService,
) {}
@Mutation(() => AgentModel)
@UseGuards(AuthGuard, AdminGuard)
async createAgent(
@Args('data') data: AgentInputModel,
): Promise<AgentModel> {
const agent = await this.prisma.agent.create({
data: {
...data,
tokenCodes: {
create: { value: 1 },
},
},
});
await this.pubSub.publish('onAgentUpdate', agent);
return agent;
}
@Mutation(() => AgentModel)
@UseGuards(AuthGuard, AdminGuard)
async updateAgent(
@Args('id') id: string,
@Args('data') data: AgentInputModel,
): Promise<AgentModel> {
const agent = await this.prisma.agent.update({
where: { id },
data,
});
await this.pubSub.publish('onAgentUpdate', agent);
return agent;
}
@Mutation(() => AgentModel)
@UseGuards(AuthGuard, AdminGuard)
async deleteAgent(@Args('id') id: string): Promise<AgentModel> {
const agent = await this.prisma.agent.delete({
where: { id },
});
await this.pubSub.publish('onAgentUpdate', {
id: agent.id,
deleted: true,
});
return agent;
}
@Mutation(() => GroupModel)
@UseGuards(AuthGuard, AdminGuard)
async createGroup(
@Args('data') data: GroupInputModel,
): Promise<GroupModel> {
const group = await this.prisma.group.create({
data,
});
await this.pubSub.publish('onGroupUpdate', group);
return group;
}
@Mutation(() => GroupModel)
@UseGuards(AuthGuard, AdminGuard)
async updateGroup(
@Args('id') id: string,
@Args('data') data: GroupInputModel,
): Promise<GroupModel> {
const group = await this.prisma.group.update({
where: { id },
data,
});
await this.pubSub.publish('onGroupUpdate', group);
return group;
}
@Mutation(() => GroupModel)
@UseGuards(AuthGuard, AdminGuard)
async deleteGroup(@Args('id') id: string): Promise<GroupModel> {
const group = await this.prisma.group.delete({
where: { id },
});
await this.pubSub.publish('onGroupUpdate', {
id: group.id,
deleted: true,
});
return group;
}
@Mutation(() => TokenCodeModel)
@UseGuards(AuthGuard, AdminGuard)
async createTokenCode(
@Args('agentId') agentId: string,
@Args('data') data: TokenCodeInputModel,
): Promise<TokenCodeModel> {
return await this.prisma.$transaction(async (prisma) => {
const codes = await prisma.tokenCode.findMany({
where: { agentId },
});
await prisma.tokenCode.deleteMany({
where: { agentId },
});
for (const code of codes) {
await this.pubSub.publish('onTokenCodeUpdate', {
id: code.id,
deleted: true,
});
}
const code = prisma.tokenCode.create({
data: {
...data,
agent: {
connect: { id: agentId },
},
},
});
await this.pubSub.publish('onTokenCodeUpdate', code);
return code;
});
}
@Mutation(() => TokenCodeModel)
@UseGuards(AuthGuard, AdminGuard)
async updateTokenCode(
@Args('data') data: TokenCodeInputModel,
): Promise<TokenCodeModel> {
const count = await this.prisma.tokenCode.count({
where: { id },
});
if (count === 0) throw new NotFoundException();
const tokenCode = await this.prisma.tokenCode.update({
where: {
id,
},
await this.pubSub.publish('onTokenCodeUpdate', tokenCode);
return tokenCode;
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
}
@Mutation(() => TokenCodeModel)
@UseGuards(AuthGuard, AdminGuard)
async deleteTokenCode(
@Args('tokenCode') tokenCode: string,
): Promise<TokenCodeModel> {
return await this.prisma.$transaction(async (prisma) => {
const toDelete = await prisma.tokenCode.findFirst({
where: { id: tokenCode },
});
if (!toDelete) throw new NotFoundException();
const agentCount = await prisma.tokenCode.count({
where: { agentId: toDelete.agentId },
});
await this.pubSub.publish('onTokenCodeUpdate', {
id: toDelete.id,
deleted: true,
});
if (agentCount <= 1) {
await prisma.tokenCode.create({
data: {
value: 1,
agent: {
connect: { id: toDelete.agentId },
},
},
});
}
const deleted = await prisma.tokenCode.delete({
where: { id: tokenCode },
});
await this.pubSub.publish('onTokenCodeUpdate', {
id: deleted.id,
deleted: true,
});
return deleted;
});
}
@Mutation(() => TokenCodeModel)
@UseGuards(AuthGuard, AdminGuard)
deleteEntry(@Args('id') id: string): Promise<EntryModel> {
return this.prisma.entry
.delete({
where: { id },
})
.then(async (entry) => {
await this.pubSub.publish('onEntryUpdate', entry);
return this.entryService.unlockedEntry(entry);
});
}
@Mutation(() => GraphQLString, { nullable: true })
@UseGuards(AuthGuard, AdminGuard)
async broadcastCommand(@Args('command') command: string) {
await this.pubSub.publish('onCommand', command);
}
}