Skip to content

Commit

Permalink
feat: support updating existing projects without existing settings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Mar 21, 2023
1 parent 600d461 commit 902ebef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/lib/db/project-store.ts
Expand Up @@ -221,19 +221,35 @@ class ProjectStore implements IProjectStore {
return this.mapRow({ ...row[0], ...settingsRow[0] });
}

private async hasProjectSettings(projectId: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${SETTINGS_TABLE} WHERE project = ?) AS present`,
[projectId],
);
const { present } = result.rows[0];
return present;
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async update(data): Promise<void> {
try {
await this.db(TABLE)
.where({ id: data.id })
.update(this.fieldToRow(data));
await this.db(SETTINGS_TABLE)
.where({ project: data.id })
.update({
if (await this.hasProjectSettings(data.id)) {
await this.db(SETTINGS_TABLE)
.where({ project: data.id })
.update({
project_mode: data.mode,
default_stickiness: data.defaultStickiness,
});
} else {
await this.db(SETTINGS_TABLE).insert({
project: data.id,
project_mode: data.mode,
default_stickiness: data.defaultStickiness,
})
.returning('*');
});
}
} catch (err) {
this.logger.error('Could not update project, error: ', err);
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/e2e/services/project-service.e2e.test.ts
Expand Up @@ -236,6 +236,35 @@ test('should update project', async () => {
expect(updatedProject.mode).toBe('protected');
});

test('should update project without existing settings', async () => {
const project = {
id: 'test-update-legacy',
name: 'New project',
description: 'Blah',
mode: 'open' as const,
};

const updatedProject = {
id: 'test-update-legacy',
name: 'New name',
description: 'Blah longer desc',
mode: 'protected' as const,
};

await projectService.createProject(project, user);
await db
.rawDatabase('project_settings')
.del()
.where({ project: project.id });
await projectService.updateProject(updatedProject, user);

const readProject = await projectService.getProject(project.id);

expect(updatedProject.name).toBe(readProject.name);
expect(updatedProject.description).toBe(readProject.description);
expect(updatedProject.mode).toBe('protected');
});

test('should give error when getting unknown project', async () => {
try {
await projectService.getProject('unknown');
Expand Down

0 comments on commit 902ebef

Please sign in to comment.