Skip to content

Commit

Permalink
Merge pull request #186 from codex-team/feat/remove-tool
Browse files Browse the repository at this point in the history
feat: added ability to remove user editor tool
  • Loading branch information
slaveeks authored Feb 23, 2024
2 parents 189efe7 + e432db2 commit b508291
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/domain/service/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,22 @@ export default class UserService {
toolId,
});
}

/**
* Removes editor tool from user settings by its id
*
* @param options - user id & editor tool
*/
public async removeUserEditorTool({
userId,
toolId,
}: {
userId: User['id'],
toolId: EditorTool['id'],
}): Promise<void> {
return await this.repository.removeUserEditorTool({
userId,
toolId,
});
}
}
47 changes: 47 additions & 0 deletions src/presentation/http/router/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,53 @@ const UserRouter: FastifyPluginCallback<UserRouterOptions> = (fastify, opts, don
});
});

/**
* Remove editor tool from user extensions
*/
fastify.delete<{
Body: { toolId: string }
}>('/editor-tools', {
config: {
policy: [
'authRequired',
],
},
schema: {
body: {
toolId: {
type: 'string',
description: 'Unique editor tool id',
},
},
response: {
'2xx': {
description: 'Removed editor tool id',
content: {
'application/json': {
schema: {
removedId: {
type: 'string',
},
},
},
},
},
},
},
}, async (request, reply) => {
const toolId = request.body.toolId;
const userId = request.userId as number;

await userService.removeUserEditorTool({
userId,
toolId,
});

return reply.send({
removedId: toolId,
});
});

done();
};

Expand Down
11 changes: 8 additions & 3 deletions src/repository/storage/postgres/orm/sequelize/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Sequelize, InferAttributes, InferCreationAttributes, CreationOptional } from 'sequelize';
import { fn, col } from 'sequelize';
import { literal } from 'sequelize';
import { Model, DataTypes } from 'sequelize';
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
import type User from '@domain/entities/user.js';
Expand Down Expand Up @@ -176,7 +176,12 @@ export default class UserSequelizeStorage {
toolId,
}: AddUserToolOptions): Promise<void> {
await this.model.update({
editorTools: fn('array_append', col('editor_tools'), toolId),
editorTools: literal(
/**
* If editorTools is null, then set it to empty array
* Then add the tool to the list
*/
`COALESCE(editor_tools, '[]'::jsonb) || '["${toolId}"]'::jsonb`),
}, {
where: {
id: userId,
Expand All @@ -195,7 +200,7 @@ export default class UserSequelizeStorage {
toolId,
}: RemoveUserEditorTool): Promise<void> {
await this.model.update({
editorTools: fn('array_remove', col('editor_tools'), toolId),
editorTools: literal(`editor_tools - '${toolId}'`),
}, {
where: {
id: userId,
Expand Down

0 comments on commit b508291

Please sign in to comment.