From 80196b732aa96532dcfb858e5774433cf11aea29de55841f9ded4de2adfab067 Mon Sep 17 00:00:00 2001 From: aslan Date: Wed, 7 Jan 2026 17:13:56 -0500 Subject: [PATCH] Refactor part 2 --- package-lock.json | 4 ++-- src/controllers/community/community.ts | 3 +++ src/controllers/invite/types.ts | 23 +++++++++++++---------- src/controllers/role/role.ts | 7 +++++++ src/controllers/role/types.ts | 14 ++++++++++++-- src/controllers/session/types.ts | 13 ++++++++----- src/controllers/user/user.ts | 6 ++++++ tests/1.user.test.js | 7 +++++++ tests/2.community.test.js | 26 ++++++++++++++++++++------ 9 files changed, 78 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index e3cbc03..a23a5b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "tether", - "version": "0.3.4", + "version": "0.3.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "tether", - "version": "0.3.4", + "version": "0.3.6", "license": "GPL-3.0-only", "dependencies": { "@fastify/cookie": "^11.0.2", diff --git a/src/controllers/community/community.ts b/src/controllers/community/community.ts index b231810..2de1ed6 100644 --- a/src/controllers/community/community.ts +++ b/src/controllers/community/community.ts @@ -84,6 +84,7 @@ const postCreateCommunity = async ( name: community.name, description: community.description, ownerId: community.ownerId, + creationDate: community.creationDate.getTime(), } as IPostCreateCommunityResponseSuccess; }; @@ -116,6 +117,8 @@ const patchCommunity = async (request: FastifyRequest, reply: FastifyReply) => { id: community.id, name: community.name, description: community.description, + ownerId: community.ownerId, + creationDate: community.creationDate.getTime(), } as IPatchCommunityResponseSuccess; }; diff --git a/src/controllers/invite/types.ts b/src/controllers/invite/types.ts index 8a49b26..60f07fd 100644 --- a/src/controllers/invite/types.ts +++ b/src/controllers/invite/types.ts @@ -1,15 +1,6 @@ import type { API_ERROR } from "../errors.js"; -interface IGetInviteParams { - id: string; -} - -interface IGetInviteResponseError { - id: string; - error: API_ERROR; -} - -interface IGetInviteResponseSuccess { +interface IInvite { id: string; communityId: string; valid: boolean; @@ -21,6 +12,17 @@ interface IGetInviteResponseSuccess { expirationDate: number; } +interface IGetInviteParams { + id: string; +} + +interface IGetInviteResponseError { + id: string; + error: API_ERROR; +} + +interface IGetInviteResponseSuccess extends IInvite {} + interface IDeleteInviteParams { id: string; } @@ -53,6 +55,7 @@ interface IPostAcceptDeleteInviteResponseSuccess { } export { + type IInvite, type IGetInviteParams, type IGetInviteResponseError, type IGetInviteResponseSuccess, diff --git a/src/controllers/role/role.ts b/src/controllers/role/role.ts index 7dc55e2..33492ff 100644 --- a/src/controllers/role/role.ts +++ b/src/controllers/role/role.ts @@ -55,7 +55,9 @@ const getRole = async (request: FastifyRequest, reply: FastifyReply) => { return { id: role.id, name: role.name, + description: role.description, communityId: role.communityId, + permissions: role.permissions, creationDate: role.creationDate.getTime(), } as IGetRoleResponseSuccess; }; @@ -75,7 +77,10 @@ const postCreateRole = async (request: FastifyRequest, reply: FastifyReply) => { return { id: role.id, name: role.name, + description: role.description, communityId: role.communityId, + permissions: role.permissions, + creationDate: role.creationDate.getTime(), } as IPostCreateRoleResponseSuccess; }; @@ -103,8 +108,10 @@ const patchRole = async (request: FastifyRequest, reply: FastifyReply) => { return { id: role.id, name: role.name, + description: role.description, communityId: role.communityId, permissions: role.permissions, + creationDate: role.creationDate.getTime(), } as IPatchRoleResponseSuccess; }; diff --git a/src/controllers/role/types.ts b/src/controllers/role/types.ts index 498b875..a9c2eab 100644 --- a/src/controllers/role/types.ts +++ b/src/controllers/role/types.ts @@ -78,7 +78,12 @@ interface IPostAssignRoleResponseError { error: API_ERROR; } -interface IPostAssignRoleResponseSuccess extends IRole {} +interface IPostAssignRoleResponseSuccess { + id: string; + name: string; + communityId: string; + userId: string; +} interface IPostUnassignRoleParams { id: string; @@ -93,7 +98,12 @@ interface IPostUnassignRoleResponseError { error: API_ERROR; } -interface IPostUnassignRoleResponseSuccess extends IRole {} +interface IPostUnassignRoleResponseSuccess { + id: string; + name: string; + communityId: string; + userId: string; +} export { type IRole, diff --git a/src/controllers/session/types.ts b/src/controllers/session/types.ts index 689ff4c..f2791a6 100644 --- a/src/controllers/session/types.ts +++ b/src/controllers/session/types.ts @@ -1,5 +1,11 @@ import type { API_ERROR } from "../errors.js"; +interface ISession { + id: string; + userId: string; + creationDate: number; +} + interface IGetSessionParams { id: string; } @@ -9,11 +15,7 @@ interface IGetSessionResponseError { error: API_ERROR; } -interface IGetSessionResponseSuccess { - id: string; - userId: string; - creationDate: number; -} +interface IGetSessionResponseSuccess extends ISession {} interface IDeleteSessionParams { id: string; @@ -30,6 +32,7 @@ interface IDeleteSessionResponseSuccess { } export { + type ISession, type IGetSessionParams, type IGetSessionResponseError, type IGetSessionResponseSuccess, diff --git a/src/controllers/user/user.ts b/src/controllers/user/user.ts index 7d2b729..beb44c6 100644 --- a/src/controllers/user/user.ts +++ b/src/controllers/user/user.ts @@ -98,6 +98,8 @@ const postCreateUser = async (request: FastifyRequest, reply: FastifyReply) => { email: user.email, description: user.description, admin: user.admin, + registerDate: user.registerDate.getTime(), + lastLogin: user.lastLogin?.getTime() ?? 0, } as IPostCreateUserResponseSuccess; }; @@ -124,8 +126,12 @@ const patchUser = async (request: FastifyRequest, reply: FastifyReply) => { return { id: user.id, + username: user.username, email: user.email, description: user.description, + admin: user.admin, + registerDate: user.registerDate.getTime(), + lastLogin: user.lastLogin?.getTime() ?? 0, } as IPatchUserResponseSuccess; }; diff --git a/tests/1.user.test.js b/tests/1.user.test.js index 6ca4464..d4b1e2c 100644 --- a/tests/1.user.test.js +++ b/tests/1.user.test.js @@ -91,6 +91,10 @@ test("can get user", async () => { assert.equal(response.id, state.userId); assert.equal(response.username, state.username); assert.equal(response.email, state.email); + assert.equal(response.description, null); + assert.equal(response.admin, false); + assert.notEqual(response.registerDate, undefined); + assert.notEqual(response.lastLogin, undefined); }); test("can modify user", async () => { @@ -113,6 +117,9 @@ test("can modify user", async () => { assert.equal(responseGet.id, state.userId); assert.equal(responseGet.email, state.email); assert.equal(responseGet.description, state.description); + assert.equal(responseGet.admin, false); + assert.notEqual(responseGet.registerDate, undefined); + assert.notEqual(responseGet.lastLogin, undefined); }); test("can get user sessions", async () => { diff --git a/tests/2.community.test.js b/tests/2.community.test.js index 052599f..d53cc97 100644 --- a/tests/2.community.test.js +++ b/tests/2.community.test.js @@ -71,11 +71,13 @@ test("can create community", async () => { assert.equal(responseCreate.name, state.communityName); assert.equal(responseCreate.description, state.communityDescription); assert.equal(responseCreate.ownerId, state.userId1); + assert.notEqual(responseCreate.creationDate, undefined); const responseGet = await apiGet(`community/${state.communityId}`); assert.equal(responseGet.name, state.communityName); assert.equal(responseGet.description, state.communityDescription); assert.equal(responseGet.ownerId, state.userId1); + assert.notEqual(responseGet.creationDate, undefined); }); test("shouldn't be able to create invite", async () => { @@ -133,7 +135,7 @@ test("can accept invite", async () => { assert.equal(responseGet.id, state.communityId); assert.equal(responseGet.name, state.communityName); - assert.equal(responseGet.members.length === 2, true); + assert.equal(responseGet.members.length, 2); }); test("can get user communities", async () => { @@ -143,7 +145,7 @@ test("can get user communities", async () => { ); assert.equal(response.id, state.userId2); - assert.equal(response.communities.length === 1, true); + assert.equal(response.communities.length, 1); assert.equal(response.communities[0].id, state.communityId); assert.equal(response.communities[0].name, state.communityName); assert.equal( @@ -193,11 +195,13 @@ test("shouldn't be able to get roles", async () => { test("can create channel", async () => { state.channelName = "Test Channel"; + state.channelDescription = "Test Channel Desc"; const response = await apiPost( `channel`, { name: state.channelName, + description: state.channelDescription, communityId: state.communityId, }, state.token1, @@ -205,18 +209,22 @@ test("can create channel", async () => { assert.equal(validate(response.id), true); assert.equal(response.name, state.channelName); + assert.equal(response.description, state.channelDescription); assert.equal(response.communityId, state.communityId); + assert.notEqual(response.creationDate, undefined); state.channelId = response.id; }); test("can create role", async () => { state.roleName = "Test Role"; + state.roleDescription = "Test Role Desc"; const response = await apiPost( `role`, { name: state.roleName, + description: state.roleDescription, communityId: state.communityId, permissions: ["MEMBERS_READ", "CHANNELS_READ", "ROLES_READ"], }, @@ -225,7 +233,10 @@ test("can create role", async () => { assert.equal(validate(response.id), true); assert.equal(response.name, state.roleName); + assert.equal(response.description, state.roleDescription); assert.equal(response.communityId, state.communityId); + assert.equal(response.permissions.length, 3); + assert.notEqual(response.creationDate, undefined); state.roleId = response.id; }); @@ -253,7 +264,7 @@ test("can get members", async () => { assert.equal(response.id, state.communityId); assert.equal(response.name, state.communityName); - assert.equal(response.members.length === 2, true); + assert.equal(response.members.length, 2); }); test("can get channels", async () => { @@ -264,7 +275,7 @@ test("can get channels", async () => { assert.equal(response.id, state.communityId); assert.equal(response.name, state.communityName); - assert.equal(response.channels.length === 1, true); + assert.equal(response.channels.length, 1); }); test("can get roles", async () => { @@ -275,7 +286,7 @@ test("can get roles", async () => { assert.equal(response.id, state.communityId); assert.equal(response.name, state.communityName); - assert.equal(response.roles.length === 1, true); + assert.equal(response.roles.length, 1); }); test("can get permissions", async () => { @@ -283,8 +294,11 @@ test("can get permissions", async () => { assert.equal(response.id, state.roleId); assert.equal(response.name, state.roleName); + assert.equal(response.description, state.roleDescription); assert.equal(response.communityId, state.communityId); - assert.equal(response.permissions.length === 3, true); + assert.equal(response.permissions.length, 3); + assert.equal(response.permissions[1], "CHANNELS_READ"); + assert.notEqual(response.creationDate, undefined); }); test("can unassign role from user", async () => {