Skip to content

Commit 58f18cd

Browse files
authored
Merge pull request #3188 from SeedCompany/bugfix/unavailability
2 parents 1d10c52 + b3e4a0f commit 58f18cd

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

dbschema/migrations/00005.edgeql

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dbschema/user.esdl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ module User {
4646
type Unavailability extending default::Resource {
4747
required description: str;
4848
required dates: range<datetime>;
49+
`start` := assert_exists(range_get_lower(.dates));
50+
`end` := assert_exists(range_get_upper(.dates));
4951
}
5052

5153
scalar type Status extending enum<Active, Disabled>;

src/components/user/unavailability/unavailability.edgedb.repository.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Injectable } from '@nestjs/common';
22
import { Range } from 'edgedb';
33
import { ID, PublicOf } from '~/common';
4-
import { e, RepoFor } from '~/core/edgedb';
4+
import { e, RepoFor, ScopeOf } from '~/core/edgedb';
55
import {
66
CreateUnavailability,
77
Unavailability,
8+
UnavailabilityListInput,
89
UpdateUnavailability,
910
} from './dto';
1011
import { UnavailabilityRepository } from './unavailability.repository';
@@ -14,8 +15,6 @@ export class UnavailabilityEdgeDBRepository
1415
extends RepoFor(Unavailability, {
1516
hydrate: (unavailability) => ({
1617
...unavailability['*'],
17-
start: e.assert_exists(e.range_get_lower(unavailability.dates)),
18-
end: e.assert_exists(e.range_get_upper(unavailability.dates)),
1918
}),
2019
}).customize((cls) => {
2120
return class extends cls {
@@ -32,7 +31,7 @@ export class UnavailabilityEdgeDBRepository
3231
}));
3332
const query = e.select(inserted, (u) => ({
3433
...this.hydrate(u),
35-
updatedUser, // Attach to query, so it is executed.
34+
updatedUser: e.alias(updatedUser), // Attach to query, so it is executed.
3635
}));
3736
return await this.db.run(query);
3837
}
@@ -67,4 +66,18 @@ export class UnavailabilityEdgeDBRepository
6766
);
6867
return await this.db.run(query);
6968
}
69+
70+
protected listFilters(
71+
unavailability: ScopeOf<typeof e.User.Unavailability>,
72+
{ filter: input }: UnavailabilityListInput,
73+
) {
74+
return [
75+
input.userId &&
76+
e.op(
77+
e.cast(e.User, e.uuid(input.userId)),
78+
'in',
79+
unavailability['<unavailabilities[is User]'],
80+
),
81+
];
82+
}
7083
}

src/core/edgedb/dto.repository.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LazyGetter as Once } from 'lazy-get-decorator';
1212
import { lowerCase } from 'lodash';
1313
import { AbstractClass } from 'type-fest';
1414
import {
15+
ClientException,
1516
DBName,
1617
EnhancedResource,
1718
EnumType,
@@ -162,7 +163,11 @@ export const RepoFor = <
162163
scope: ScopeOf<Root>,
163164
input: SortablePaginationInput,
164165
): OrderByExpression {
165-
// TODO Validate this is a valid sort key
166+
if (!(input.sort in scope.__element__.__pointers__)) {
167+
throw new ClientException(
168+
`'${input.sort}' is not a valid sort key for '${resource.name}'`,
169+
);
170+
}
166171
return {
167172
expression: (scope as any)[input.sort],
168173
direction: input.order,

0 commit comments

Comments
 (0)