/
app.controller.ts
102 lines (97 loc) · 2.74 KB
/
app.controller.ts
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
import { Controller, Get, Req, Res, Sse, UseGuards } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Request, Response } from 'express';
import { PrismaService } from 'nestjs-prisma';
import { map, Subject } from 'rxjs';
import { AppService } from './app.service';
import { EventsJwtAuthGuard } from './auth/guards/events-jwt-auth.guard';
import { EVENTS } from './common/events';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly eventService: EventEmitter2,
private readonly prisma: PrismaService
) {}
@Get()
goHome(@Res() res: Response) {
return res.redirect('https://altairgraphql.dev');
}
@UseGuards(EventsJwtAuthGuard)
@Sse('events')
handleUserEvents(@Req() req: Request) {
const subject$ = new Subject();
const userId = req.user.id;
// TODO: Create events
// TODO: Emit events from prisma middleware
this.eventService.on([EVENTS.COLLECTION_UPDATE], async ({ id }) => {
const validUserCollection = await this.prisma.queryCollection.findFirst({
select: {
id: true,
},
where: {
id,
OR: [
{
// queries user owns
workspace: {
ownerId: userId,
},
},
{
// queries owned by user's team
workspace: {
team: {
TeamMemberships: {
some: {
userId,
},
},
},
},
},
],
},
});
if (validUserCollection) {
subject$.next({ uid: userId, collectionId: id });
}
});
this.eventService.on([EVENTS.QUERY_UPDATE], async ({ id }) => {
// check query workspace owner
const validQueryItem = await this.prisma.queryItem.findFirst({
where: {
AND: {
id,
collection: {
OR: [
{
// queries user owns
workspace: {
ownerId: userId,
},
},
{
// queries owned by user's team
workspace: {
team: {
TeamMemberships: {
some: {
userId,
},
},
},
},
},
],
},
},
},
});
if (validQueryItem) {
subject$.next({ uid: req.user.id, queryId: id });
}
});
return subject$.pipe(map((data) => ({ data })));
}
}