Skip to content

Commit 9033f41

Browse files
committed
Move "root" directory creation logic to FileService
The previous logic stopped working because we started assuming with hydrate that a FileNode would have an attachment point, aka not be orphaned. With the Project root directory previous process, it would be "orphaned" at first until the next query ran relating the node to the project. Now it's just created all at once, which is better anyways.
1 parent 0f6c558 commit 9033f41

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

src/components/file/file.repository.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
ILogger,
2727
Logger,
2828
OnIndex,
29+
ResourceRef,
2930
} from '../../core';
3031
import {
3132
ACTIVE,
@@ -406,6 +407,42 @@ export class FileRepository extends CommonRepository {
406407
return result.id;
407408
}
408409

410+
async createRootDirectory({
411+
resource,
412+
relation,
413+
name,
414+
public: isPublic,
415+
session,
416+
}: {
417+
resource: ResourceRef<any>;
418+
relation: string;
419+
name: string;
420+
public?: boolean;
421+
session: Session;
422+
}) {
423+
const initialProps = {
424+
name,
425+
public: isPublic,
426+
};
427+
428+
const query = this.db
429+
.query()
430+
.apply(await createNode(Directory, { initialProps }))
431+
.apply(
432+
createRelationships(Directory, {
433+
in: { [relation]: ['BaseNode', resource.id] },
434+
out: { createdBy: ['User', session.userId] },
435+
}),
436+
)
437+
.return<{ id: ID }>('node.id as id');
438+
439+
const result = await query.first();
440+
if (!result) {
441+
throw new ServerException('Failed to create directory');
442+
}
443+
return result.id;
444+
}
445+
409446
async createFile({
410447
fileId,
411448
name,

src/components/file/file.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ export class FileService {
227227
return await this.getDirectory(id, session);
228228
}
229229

230+
async createRootDirectory(
231+
...args: Parameters<FileRepository['createRootDirectory']>
232+
) {
233+
return await this.repo.createRootDirectory(...args);
234+
}
235+
230236
async requestUpload(): Promise<RequestUploadOutput> {
231237
const id = await generateId();
232238
const url = await this.bucket.getSignedUrl(PutObject, {
Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { node, relation } from 'cypher-query-builder';
2-
import { DateTime } from 'luxon';
3-
import { DatabaseService, EventsHandler, IEventHandler } from '../../../core';
1+
import { DatabaseService, EventsHandler, IEventHandler } from '~/core';
42
import { ProjectCreatedEvent } from '../../project/events';
53
import { FileService } from '../file.service';
64

@@ -15,32 +13,17 @@ export class AttachProjectRootDirectoryHandler
1513

1614
async handle(event: ProjectCreatedEvent) {
1715
const { project, session } = event;
18-
const { id } = project;
1916

20-
const rootDir = await this.files.createDirectory(
21-
undefined,
22-
`${id} root directory`,
17+
const rootDirId = await this.files.createRootDirectory({
18+
resource: project,
19+
relation: 'rootDirectory',
20+
name: `${project.id} root directory`,
2321
session,
24-
);
22+
});
2523

26-
await this.db
27-
.query()
28-
.match([
29-
[node('project', 'Project', { id })],
30-
[node('dir', 'Directory', { id: rootDir.id })],
31-
])
32-
.create([
33-
node('project'),
34-
relation('out', '', 'rootDirectory', {
35-
active: true,
36-
createdAt: DateTime.local(),
37-
}),
38-
node('dir'),
39-
])
40-
.run();
4124
event.project = {
4225
...event.project,
43-
rootDirectory: rootDir.id,
26+
rootDirectory: rootDirId,
4427
};
4528

4629
const folders = [
@@ -50,7 +33,7 @@ export class AttachProjectRootDirectoryHandler
5033
'Photos',
5134
];
5235
for (const folder of folders) {
53-
await this.files.createDirectory(rootDir.id, folder, session);
36+
await this.files.createDirectory(rootDirId, folder, session);
5437
}
5538
}
5639
}

test/utility/create-directory.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export async function createRootDirectory(app: TestApp, name?: string) {
1414
.get(AuthenticationService)
1515
.resumeSession(app.graphql.authToken);
1616
const session = loggedInSession(rawSession);
17-
const actual = await app
18-
.get(FileService)
19-
.createDirectory(undefined, name, session);
20-
21-
expect(actual).toBeTruthy();
22-
expect(actual.name).toBe(name);
23-
24-
return actual;
17+
const id = await app.get(FileService).createRootDirectory({
18+
// An attachment point is required, so just use the current user.
19+
resource: { __typename: 'User', id: session.userId },
20+
relation: 'dir',
21+
name,
22+
session,
23+
});
24+
return await app.get(FileService).getDirectory(id, session);
2525
}
2626

2727
export async function createDirectory(

0 commit comments

Comments
 (0)