|
1 | 1 | import { Injectable } from '@nestjs/common';
|
| 2 | +import { NotImplementedException, PublicOf } from '~/common'; |
| 3 | +import { e, RepoFor, ScopeOf } from '~/core/edgedb'; |
2 | 4 | import {
|
3 |
| - DuplicateException, |
4 |
| - ID, |
5 |
| - NotFoundException, |
6 |
| - ServerException, |
7 |
| - Session, |
8 |
| -} from '~/common'; |
9 |
| -import { e, EdgeDB, isExclusivityViolation } from '~/core/edgedb'; |
10 |
| -import { CreatePerson, User, UserListInput } from './dto'; |
11 |
| -import { UserRepository } from './user.repository'; |
12 |
| - |
13 |
| -const hydrate = e.shape(e.User, (user) => ({ |
14 |
| - ...user['*'], |
15 |
| - // Other links if needed |
16 |
| -})); |
| 5 | + AssignOrganizationToUser, |
| 6 | + RemoveOrganizationFromUser, |
| 7 | + User, |
| 8 | + UserListInput, |
| 9 | +} from './dto'; |
| 10 | +import type { UserRepository } from './user.repository'; |
17 | 11 |
|
18 | 12 | @Injectable()
|
19 |
| -export class UserEdgedbRepository extends UserRepository { |
20 |
| - constructor(private readonly edgedb: EdgeDB) { |
21 |
| - super(); |
22 |
| - } |
23 |
| - |
24 |
| - async readOne(id: ID, _session: Session | ID) { |
25 |
| - const query = e.select(e.User, (user) => ({ |
26 |
| - ...hydrate(user), |
27 |
| - filter_single: { id }, |
28 |
| - })); |
29 |
| - const user = await this.edgedb.run(query); |
30 |
| - if (!user) { |
31 |
| - throw new NotFoundException('Could not find user'); |
32 |
| - } |
33 |
| - return user; |
34 |
| - } |
35 |
| - |
36 |
| - async readMany(ids: readonly ID[], _session: Session | ID) { |
37 |
| - const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) => |
38 |
| - e.select(e.User, (user) => ({ |
39 |
| - ...hydrate(user), |
40 |
| - filter: e.op(user.id, 'in', e.array_unpack(ids)), |
41 |
| - })), |
42 |
| - ); |
43 |
| - const users = await this.edgedb.run(query, { ids }); |
44 |
| - return users; |
45 |
| - } |
46 |
| - |
| 13 | +export class UserEdgeDBRepository |
| 14 | + extends RepoFor(User).withDefaults() |
| 15 | + implements PublicOf<UserRepository> |
| 16 | +{ |
47 | 17 | async doesEmailAddressExist(email: string) {
|
48 | 18 | const query = e.select(e.User, () => ({
|
49 | 19 | filter_single: { email },
|
50 | 20 | }));
|
51 |
| - const result = await this.edgedb.run(query); |
| 21 | + const result = await this.db.run(query); |
52 | 22 | return !!result;
|
53 | 23 | }
|
54 | 24 |
|
55 |
| - async list(input: UserListInput, _session: Session) { |
56 |
| - const sortKey = input.sort as keyof (typeof e.User)['*']; |
57 |
| - const all = e.select(e.User, (user) => ({ |
58 |
| - filter: e.all( |
59 |
| - input.filter.pinned != null |
60 |
| - ? e.op(user.pinned, '=', input.filter.pinned) |
61 |
| - : true, |
62 |
| - // More filters here when needed... |
63 |
| - ), |
64 |
| - order_by: { |
65 |
| - expression: user[sortKey], |
66 |
| - direction: input.order, |
67 |
| - }, |
68 |
| - })); |
69 |
| - const thisPage = e.select(all, () => ({ |
70 |
| - offset: (input.page - 1) * input.count, |
71 |
| - limit: input.count + 1, |
72 |
| - })); |
73 |
| - const query = e.select({ |
74 |
| - items: e.select(thisPage, (user) => ({ |
75 |
| - ...hydrate(user), |
76 |
| - limit: input.count, |
77 |
| - })), |
78 |
| - total: e.count(all), |
79 |
| - hasMore: e.op(e.count(thisPage), '>', input.count), |
80 |
| - }); |
81 |
| - return await this.edgedb.run(query); |
| 25 | + protected listFilters(user: ScopeOf<typeof e.User>, input: UserListInput) { |
| 26 | + return [ |
| 27 | + input.filter.pinned != null && |
| 28 | + e.op(user.pinned, '=', input.filter.pinned), |
| 29 | + // More filters here when needed... |
| 30 | + ]; |
82 | 31 | }
|
83 | 32 |
|
84 |
| - async create(input: CreatePerson) { |
85 |
| - const query = e.insert(e.User, { ...input }); |
86 |
| - try { |
87 |
| - const result = await this.edgedb.run(query); |
88 |
| - return result.id; |
89 |
| - } catch (e) { |
90 |
| - if (isExclusivityViolation(e, 'email')) { |
91 |
| - throw new DuplicateException( |
92 |
| - 'person.email', |
93 |
| - 'Email address is already in use', |
94 |
| - e, |
95 |
| - ); |
96 |
| - } |
97 |
| - throw new ServerException('Failed to create user', e); |
98 |
| - } |
| 33 | + assignOrganizationToUser(args: AssignOrganizationToUser): Promise<void> { |
| 34 | + throw new NotImplementedException().with(args); |
| 35 | + } |
| 36 | + removeOrganizationFromUser(args: RemoveOrganizationFromUser): Promise<void> { |
| 37 | + throw new NotImplementedException().with(args); |
99 | 38 | }
|
100 | 39 |
|
101 |
| - async delete(id: ID, _session: Session, _object: User): Promise<void> { |
102 |
| - const query = e.delete(e.User, () => ({ |
103 |
| - filter_single: { id }, |
104 |
| - })); |
105 |
| - try { |
106 |
| - await this.edgedb.run(query); |
107 |
| - } catch (exception) { |
108 |
| - throw new ServerException('Failed to delete', exception); |
109 |
| - } |
| 40 | + hydrateAsNeo4j(_session: unknown): any { |
| 41 | + throw new NotImplementedException(); |
110 | 42 | }
|
111 | 43 | }
|
0 commit comments