Skip to content

Commit 8ba135b

Browse files
committed
Change User EdgeDB Repo to extend from new EdgeDB Dto Repo
1 parent b2ec0d7 commit 8ba135b

File tree

4 files changed

+41
-80
lines changed

4 files changed

+41
-80
lines changed
Lines changed: 31 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,46 @@
11
import { Injectable } from '@nestjs/common';
2-
import { ID, NotFoundException, Session } from '~/common';
3-
import { e, EdgeDB } from '~/core/edgedb';
4-
import { CreatePerson, User, UserListInput } from './dto';
5-
import { UserRepository } from './user.repository';
6-
7-
const hydrate = e.shape(e.User, (user) => ({
8-
...user['*'],
9-
// Other links if needed
10-
}));
2+
import { NotImplementedException, PublicOf } from '~/common';
3+
import { e, RepoFor, ScopeOf } from '~/core/edgedb';
4+
import {
5+
AssignOrganizationToUser,
6+
RemoveOrganizationFromUser,
7+
User,
8+
UserListInput,
9+
} from './dto';
10+
import type { UserRepository } from './user.repository';
1111

1212
@Injectable()
13-
export class UserEdgedbRepository extends UserRepository {
14-
constructor(private readonly edgedb: EdgeDB) {
15-
super();
16-
}
17-
18-
async readOne(id: ID, _session: Session | ID) {
19-
const query = e.select(e.User, (user) => ({
20-
...hydrate(user),
21-
filter_single: { id },
22-
}));
23-
const user = await this.edgedb.run(query);
24-
if (!user) {
25-
throw new NotFoundException('Could not find user');
26-
}
27-
return user;
28-
}
29-
30-
async readMany(ids: readonly ID[], _session: Session | ID) {
31-
const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) =>
32-
e.select(e.User, (user) => ({
33-
...hydrate(user),
34-
filter: e.op(user.id, 'in', e.array_unpack(ids)),
35-
})),
36-
);
37-
const users = await this.edgedb.run(query, { ids });
38-
return users;
39-
}
40-
13+
export class UserEdgeDBRepository
14+
extends RepoFor(User).withDefaults()
15+
implements
16+
Omit<
17+
PublicOf<UserRepository>,
18+
// hydrate is public (not default) and specific to Neo4j,
19+
// but it will only be called by other neo4j repositories,
20+
// so it doesn't have to match here
21+
'hydrate'
22+
>
23+
{
4124
async doesEmailAddressExist(email: string) {
4225
const query = e.select(e.User, () => ({
4326
filter_single: { email },
4427
}));
45-
const result = await this.edgedb.run(query);
28+
const result = await this.db.run(query);
4629
return !!result;
4730
}
4831

49-
async list(input: UserListInput, _session: Session) {
50-
const sortKey = input.sort as keyof (typeof e.User)['*'];
51-
const all = e.select(e.User, (user) => ({
52-
filter: e.all(
53-
input.filter.pinned != null
54-
? e.op(user.pinned, '=', input.filter.pinned)
55-
: true,
56-
// More filters here when needed...
57-
),
58-
order_by: {
59-
expression: user[sortKey],
60-
direction: input.order,
61-
},
62-
}));
63-
const thisPage = e.select(all, () => ({
64-
offset: (input.page - 1) * input.count,
65-
limit: input.count + 1,
66-
}));
67-
const query = e.select({
68-
items: e.select(thisPage, (user) => ({
69-
...hydrate(user),
70-
limit: input.count,
71-
})),
72-
total: e.count(all),
73-
hasMore: e.op(e.count(thisPage), '>', input.count),
74-
});
75-
return await this.edgedb.run(query);
32+
protected listFilters(user: ScopeOf<typeof e.User>, input: UserListInput) {
33+
return [
34+
input.filter.pinned != null &&
35+
e.op(user.pinned, '=', input.filter.pinned),
36+
// More filters here when needed...
37+
];
7638
}
7739

78-
async create(input: CreatePerson) {
79-
const query = e.insert(e.User, { ...input });
80-
const result = await this.edgedb.run(query);
81-
return result.id;
40+
assignOrganizationToUser(args: AssignOrganizationToUser): Promise<void> {
41+
throw new NotImplementedException().with(args);
8242
}
83-
84-
async delete(id: ID, _session: Session, _object: User): Promise<void> {
85-
const query = e.delete(e.User, () => ({
86-
filter_single: { id },
87-
}));
88-
await this.edgedb.run(query);
43+
removeOrganizationFromUser(args: RemoveOrganizationFromUser): Promise<void> {
44+
throw new NotImplementedException().with(args);
8945
}
9046
}

src/components/user/user.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { EducationModule } from './education/education.module';
1212
import { KnownLanguageRepository } from './known-language.repository';
1313
import { KnownLanguageResolver } from './known-language.resolver';
1414
import { UnavailabilityModule } from './unavailability/unavailability.module';
15-
import { UserEdgedbRepository } from './user.edgedb.repository';
15+
import { UserEdgeDBRepository } from './user.edgedb.repository';
1616
import { UserLoader } from './user.loader';
1717
import { UserRepository } from './user.repository';
1818
import { UserResolver } from './user.resolver';
@@ -36,7 +36,7 @@ import { UserService } from './user.service';
3636
AssignableRolesResolver,
3737
UserLoader,
3838
UserService,
39-
splitDb(UserRepository, UserEdgedbRepository),
39+
splitDb(UserRepository, UserEdgeDBRepository as any),
4040
KnownLanguageRepository,
4141
],
4242
exports: [UserService, UserRepository, EducationModule, UnavailabilityModule],

src/components/user/user.repository.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ export class UserRepository extends DtoRepository<typeof User, [Session | ID]>(
8484
if (!result) {
8585
throw new ServerException('Failed to create user');
8686
}
87-
return result.id;
87+
return result;
8888
}
8989

90-
async update(existing: User, changes: ChangesOf<User, UpdateUser>) {
90+
async update(
91+
existing: User,
92+
changes: ChangesOf<User, UpdateUser>,
93+
): Promise<unknown> {
9194
const { roles, email, ...simpleChanges } = changes;
9295

9396
await this.updateProperties(existing, simpleChanges);
@@ -97,6 +100,8 @@ export class UserRepository extends DtoRepository<typeof User, [Session | ID]>(
97100
if (roles) {
98101
await this.updateRoles(existing, roles);
99102
}
103+
104+
return undefined;
100105
}
101106

102107
hydrate(requestingUserId: Session | ID) {

src/components/user/user.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class UserService {
7979
this.verifyRolesAreAssignable(session, input.roles);
8080
}
8181

82-
const id = await this.userRepo.create(input);
82+
const { id } = await this.userRepo.create(input);
8383
return id;
8484
}
8585

0 commit comments

Comments
 (0)