Skip to content

Commit c811673

Browse files
authored
Merge pull request #3175 from SeedCompany/bugfix/edgedb-stuff
2 parents faf82f8 + 4b3d4c2 commit c811673

File tree

10 files changed

+79
-26
lines changed

10 files changed

+79
-26
lines changed

dbschema/language.esdl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ module default {
7171
(exists .firstScriptureEngagement and not .hasExternalFirstScripture)
7272
or not exists .firstScriptureEngagement
7373
);
74-
74+
75+
multi locations: Location;
76+
7577
engagements := (
7678
# Similar to previous version but avoids https://github.com/edgedb/edgedb/issues/5846
7779
select LanguageEngagement filter __source__ = .language

dbschema/migrations/00004.edgeql

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

dbschema/organization.esdl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module default {
99
multi types: Organization::Type;
1010
multi reach: Organization::Reach;
1111

12+
multi locations: Location;
13+
1214
overloaded link projectContext: Project::Context {
1315
default := (insert Project::Context);
1416
on source delete delete target;

dbschema/seeds/004.users.edgeql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ with
162162
union (
163163
insert User::Unavailability {
164164
description := <str>unavailability['description'],
165-
dates := range(<cal::local_date>unavailability['start'], <cal::local_date>unavailability['end'])
165+
dates := range(
166+
to_datetime(<cal::local_datetime><cal::local_date>unavailability['start'], 'America/Chicago'),
167+
to_datetime(<cal::local_datetime><cal::local_date>unavailability['end'], 'America/Chicago')
168+
)
166169
}
167170
)
168171
)

dbschema/user.esdl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ module default {
2626
}
2727
multi link education: User::Education {
2828
on target delete allow;
29+
on source delete delete target;
2930
}
3031
multi link unavailabilities: User::Unavailability {
3132
on target delete allow;
33+
on source delete delete target;
3234
}
35+
multi locations: Location;
3336
}
3437
}
3538

@@ -42,7 +45,7 @@ module User {
4245

4346
type Unavailability extending default::Resource {
4447
required description: str;
45-
required dates: range<cal::local_date>;
48+
required dates: range<datetime>;
4649
}
4750

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

src/components/authentication/authentication.service.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import JWT from 'jsonwebtoken';
55
import { DateTime } from 'luxon';
66
import { Writable } from 'ts-essentials';
77
import { sessionFromContext } from '~/common/session';
8+
import { disableAccessPolicies, EdgeDB } from '~/core/edgedb';
89
import {
910
DuplicateException,
1011
GqlContextType,
@@ -38,6 +39,7 @@ export class AuthenticationService {
3839
private readonly privileges: Privileges,
3940
@Logger('authentication:service') private readonly logger: ILogger,
4041
private readonly repo: AuthenticationRepository,
42+
private readonly edgedb: EdgeDB,
4143
private readonly moduleRef: ModuleRef,
4244
) {}
4345

@@ -48,7 +50,10 @@ export class AuthenticationService {
4850
return token;
4951
}
5052

51-
async register(input: RegisterInput, session?: Session): Promise<ID> {
53+
async register(
54+
{ password, ...input }: RegisterInput,
55+
session?: Session,
56+
): Promise<ID> {
5257
// ensure no other tokens are associated with this user
5358
if (session) {
5459
await this.logout(session.token);
@@ -58,7 +63,10 @@ export class AuthenticationService {
5863
try {
5964
const userMod = await import('../user');
6065
const users = this.moduleRef.get(userMod.UserService, { strict: false });
61-
userId = await users.create(input, session);
66+
userId = await this.edgedb.usingOptions(
67+
disableAccessPolicies,
68+
async () => await users.create(input, session),
69+
);
6270
} catch (e) {
6371
// remap field prop as `email` field is at a different location in register() than createPerson()
6472
if (e instanceof DuplicateException && e.field === 'person.email') {
@@ -67,7 +75,7 @@ export class AuthenticationService {
6775
throw e;
6876
}
6977

70-
const passwordHash = await this.crypto.hash(input.password);
78+
const passwordHash = await this.crypto.hash(password);
7179
await this.repo.savePasswordHashOnUser(userId, passwordHash);
7280

7381
return userId;

src/components/project/project.edgedb.repository.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Injectable, Type } from '@nestjs/common';
22
import { ModuleRef } from '@nestjs/core';
3+
import { LazyGetter } from 'lazy-get-decorator';
34
import { PublicOf, SortablePaginationInput, UnsecuredDto } from '~/common';
4-
import { grabInstances, InstanceMapOf } from '~/common/instance-maps';
5+
import { grabInstances } from '~/common/instance-maps';
56
import { ChangesOf } from '~/core/database/changes';
67
import { castToEnum, e, RepoFor, ScopeOf } from '~/core/edgedb';
78
import {
@@ -57,18 +58,21 @@ export class ProjectEdgeDBRepository
5758
})
5859
implements PublicOf<Neo4jRepository>
5960
{
60-
protected readonly concretes: InstanceMapOf<typeof ConcreteRepos>;
61-
62-
constructor(moduleRef: ModuleRef) {
61+
constructor(private readonly moduleRef: ModuleRef) {
6362
super();
64-
this.concretes = grabInstances(moduleRef, ConcreteRepos);
63+
}
64+
65+
@LazyGetter() protected get concretes() {
66+
return grabInstances(this.moduleRef, ConcreteRepos);
6567
}
6668

6769
async create(input: CreateProject) {
68-
const { type, sensitivity, ...props } = input;
70+
const { type, sensitivity, otherLocationIds, presetInventory, ...props } =
71+
input;
6972
return await this.concretes[input.type].create({
7073
...props,
7174
ownSensitivity: sensitivity,
75+
otherLocations: otherLocationIds,
7276
});
7377
}
7478

test/partnership.e2e-spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { sample, times } from 'lodash';
2+
import { v1 as uuid } from 'uuid';
23
import { CalendarDate, ID } from '../src/common';
34
import { Role } from '../src/components/authorization';
45
import { PartnerType } from '../src/components/partner';
@@ -275,7 +276,7 @@ describe('Partnership e2e', () => {
275276
await expect(
276277
createPartnership(app, {
277278
projectId: project.id,
278-
partnerId: 'fakePartner' as ID,
279+
partnerId: uuid() as ID,
279280
}),
280281
).rejects.toThrowGqlError(
281282
errors.notFound({
@@ -288,7 +289,7 @@ describe('Partnership e2e', () => {
288289
it('create partnership does not create if projectId is invalid', async () => {
289290
await expect(
290291
createPartnership(app, {
291-
projectId: 'fakeProject' as ID,
292+
projectId: uuid() as ID,
292293
}),
293294
).rejects.toThrowGqlError(
294295
errors.notFound({

test/project.e2e-spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { faker } from '@faker-js/faker';
22
import { intersection, times } from 'lodash';
33
import { DateTime } from 'luxon';
4+
import { v1 as uuid } from 'uuid';
45
import {
56
CalendarDate,
67
generateId,
@@ -201,7 +202,7 @@ describe('Project e2e', () => {
201202
createProject(app, {
202203
name: faker.string.uuid(),
203204
type: ProjectType.MomentumTranslation,
204-
fieldRegionId: 'invalid-location-id' as ID,
205+
fieldRegionId: uuid() as ID,
205206
}),
206207
).rejects.toThrowGqlError(
207208
errors.notFound({

test/utility/edgedb-setup.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ export const ephemeralEdgeDB = async () => {
77
}
88

99
const db = createClient();
10+
const config = await db.resolveConnectionParams();
11+
// Workaround old default fallback. Didn't hit locally, but did in CI.
12+
const main = config.branch === 'edgedb' ? 'main' : config.branch;
1013

1114
await dropStale(db);
1215

1316
const branch = `test_${Date.now()}`;
1417

15-
await db.execute(`create schema branch ${branch} from main`);
18+
await db.execute(`create schema branch ${branch} from ${main}`);
1619

1720
const cleanup = async () => {
1821
await db.execute(`drop branch ${branch}`);
@@ -27,21 +30,18 @@ export const ephemeralEdgeDB = async () => {
2730
async function dropStale(db: Client) {
2831
const branches = await db.query<string>('select sys::Database.name');
2932

30-
const stale = branches.flatMap((name) => {
33+
const stale = branches.filter((name) => {
3134
if (!name.startsWith('test_')) {
32-
return [];
35+
return false;
3336
}
3437
const ts = Number(name.slice(5));
3538
if (isNaN(ts)) {
36-
return [];
39+
return false;
3740
}
3841
const createdAt = DateTime.fromMillis(ts);
39-
return createdAt.diffNow().as('hours') > 4 ? name : [];
40-
});
41-
if (stale.length === 0) {
42-
return;
43-
}
44-
await db.execute('drop branch array_unpack(<array<string>>$branches)', {
45-
branches: stale,
42+
// more than 1 hour old
43+
return createdAt.diffNow().as('hours') < -1;
4644
});
45+
46+
await Promise.all(stale.map((branch) => db.execute(`drop branch ${branch}`)));
4747
}

0 commit comments

Comments
 (0)