Refactor part 2

This commit is contained in:
Aslan 2026-01-07 17:13:56 -05:00
parent e21a807fb3
commit 80196b732a
9 changed files with 78 additions and 25 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "tether", "name": "tether",
"version": "0.3.4", "version": "0.3.6",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "tether", "name": "tether",
"version": "0.3.4", "version": "0.3.6",
"license": "GPL-3.0-only", "license": "GPL-3.0-only",
"dependencies": { "dependencies": {
"@fastify/cookie": "^11.0.2", "@fastify/cookie": "^11.0.2",

View file

@ -84,6 +84,7 @@ const postCreateCommunity = async (
name: community.name, name: community.name,
description: community.description, description: community.description,
ownerId: community.ownerId, ownerId: community.ownerId,
creationDate: community.creationDate.getTime(),
} as IPostCreateCommunityResponseSuccess; } as IPostCreateCommunityResponseSuccess;
}; };
@ -116,6 +117,8 @@ const patchCommunity = async (request: FastifyRequest, reply: FastifyReply) => {
id: community.id, id: community.id,
name: community.name, name: community.name,
description: community.description, description: community.description,
ownerId: community.ownerId,
creationDate: community.creationDate.getTime(),
} as IPatchCommunityResponseSuccess; } as IPatchCommunityResponseSuccess;
}; };

View file

@ -1,15 +1,6 @@
import type { API_ERROR } from "../errors.js"; import type { API_ERROR } from "../errors.js";
interface IGetInviteParams { interface IInvite {
id: string;
}
interface IGetInviteResponseError {
id: string;
error: API_ERROR;
}
interface IGetInviteResponseSuccess {
id: string; id: string;
communityId: string; communityId: string;
valid: boolean; valid: boolean;
@ -21,6 +12,17 @@ interface IGetInviteResponseSuccess {
expirationDate: number; expirationDate: number;
} }
interface IGetInviteParams {
id: string;
}
interface IGetInviteResponseError {
id: string;
error: API_ERROR;
}
interface IGetInviteResponseSuccess extends IInvite {}
interface IDeleteInviteParams { interface IDeleteInviteParams {
id: string; id: string;
} }
@ -53,6 +55,7 @@ interface IPostAcceptDeleteInviteResponseSuccess {
} }
export { export {
type IInvite,
type IGetInviteParams, type IGetInviteParams,
type IGetInviteResponseError, type IGetInviteResponseError,
type IGetInviteResponseSuccess, type IGetInviteResponseSuccess,

View file

@ -55,7 +55,9 @@ const getRole = async (request: FastifyRequest, reply: FastifyReply) => {
return { return {
id: role.id, id: role.id,
name: role.name, name: role.name,
description: role.description,
communityId: role.communityId, communityId: role.communityId,
permissions: role.permissions,
creationDate: role.creationDate.getTime(), creationDate: role.creationDate.getTime(),
} as IGetRoleResponseSuccess; } as IGetRoleResponseSuccess;
}; };
@ -75,7 +77,10 @@ const postCreateRole = async (request: FastifyRequest, reply: FastifyReply) => {
return { return {
id: role.id, id: role.id,
name: role.name, name: role.name,
description: role.description,
communityId: role.communityId, communityId: role.communityId,
permissions: role.permissions,
creationDate: role.creationDate.getTime(),
} as IPostCreateRoleResponseSuccess; } as IPostCreateRoleResponseSuccess;
}; };
@ -103,8 +108,10 @@ const patchRole = async (request: FastifyRequest, reply: FastifyReply) => {
return { return {
id: role.id, id: role.id,
name: role.name, name: role.name,
description: role.description,
communityId: role.communityId, communityId: role.communityId,
permissions: role.permissions, permissions: role.permissions,
creationDate: role.creationDate.getTime(),
} as IPatchRoleResponseSuccess; } as IPatchRoleResponseSuccess;
}; };

View file

@ -78,7 +78,12 @@ interface IPostAssignRoleResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostAssignRoleResponseSuccess extends IRole {} interface IPostAssignRoleResponseSuccess {
id: string;
name: string;
communityId: string;
userId: string;
}
interface IPostUnassignRoleParams { interface IPostUnassignRoleParams {
id: string; id: string;
@ -93,7 +98,12 @@ interface IPostUnassignRoleResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostUnassignRoleResponseSuccess extends IRole {} interface IPostUnassignRoleResponseSuccess {
id: string;
name: string;
communityId: string;
userId: string;
}
export { export {
type IRole, type IRole,

View file

@ -1,5 +1,11 @@
import type { API_ERROR } from "../errors.js"; import type { API_ERROR } from "../errors.js";
interface ISession {
id: string;
userId: string;
creationDate: number;
}
interface IGetSessionParams { interface IGetSessionParams {
id: string; id: string;
} }
@ -9,11 +15,7 @@ interface IGetSessionResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IGetSessionResponseSuccess { interface IGetSessionResponseSuccess extends ISession {}
id: string;
userId: string;
creationDate: number;
}
interface IDeleteSessionParams { interface IDeleteSessionParams {
id: string; id: string;
@ -30,6 +32,7 @@ interface IDeleteSessionResponseSuccess {
} }
export { export {
type ISession,
type IGetSessionParams, type IGetSessionParams,
type IGetSessionResponseError, type IGetSessionResponseError,
type IGetSessionResponseSuccess, type IGetSessionResponseSuccess,

View file

@ -98,6 +98,8 @@ const postCreateUser = async (request: FastifyRequest, reply: FastifyReply) => {
email: user.email, email: user.email,
description: user.description, description: user.description,
admin: user.admin, admin: user.admin,
registerDate: user.registerDate.getTime(),
lastLogin: user.lastLogin?.getTime() ?? 0,
} as IPostCreateUserResponseSuccess; } as IPostCreateUserResponseSuccess;
}; };
@ -124,8 +126,12 @@ const patchUser = async (request: FastifyRequest, reply: FastifyReply) => {
return { return {
id: user.id, id: user.id,
username: user.username,
email: user.email, email: user.email,
description: user.description, description: user.description,
admin: user.admin,
registerDate: user.registerDate.getTime(),
lastLogin: user.lastLogin?.getTime() ?? 0,
} as IPatchUserResponseSuccess; } as IPatchUserResponseSuccess;
}; };

View file

@ -91,6 +91,10 @@ test("can get user", async () => {
assert.equal(response.id, state.userId); assert.equal(response.id, state.userId);
assert.equal(response.username, state.username); assert.equal(response.username, state.username);
assert.equal(response.email, state.email); 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 () => { test("can modify user", async () => {
@ -113,6 +117,9 @@ test("can modify user", async () => {
assert.equal(responseGet.id, state.userId); assert.equal(responseGet.id, state.userId);
assert.equal(responseGet.email, state.email); assert.equal(responseGet.email, state.email);
assert.equal(responseGet.description, state.description); 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 () => { test("can get user sessions", async () => {

View file

@ -71,11 +71,13 @@ test("can create community", async () => {
assert.equal(responseCreate.name, state.communityName); assert.equal(responseCreate.name, state.communityName);
assert.equal(responseCreate.description, state.communityDescription); assert.equal(responseCreate.description, state.communityDescription);
assert.equal(responseCreate.ownerId, state.userId1); assert.equal(responseCreate.ownerId, state.userId1);
assert.notEqual(responseCreate.creationDate, undefined);
const responseGet = await apiGet(`community/${state.communityId}`); const responseGet = await apiGet(`community/${state.communityId}`);
assert.equal(responseGet.name, state.communityName); assert.equal(responseGet.name, state.communityName);
assert.equal(responseGet.description, state.communityDescription); assert.equal(responseGet.description, state.communityDescription);
assert.equal(responseGet.ownerId, state.userId1); assert.equal(responseGet.ownerId, state.userId1);
assert.notEqual(responseGet.creationDate, undefined);
}); });
test("shouldn't be able to create invite", async () => { 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.id, state.communityId);
assert.equal(responseGet.name, state.communityName); 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 () => { 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.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].id, state.communityId);
assert.equal(response.communities[0].name, state.communityName); assert.equal(response.communities[0].name, state.communityName);
assert.equal( assert.equal(
@ -193,11 +195,13 @@ test("shouldn't be able to get roles", async () => {
test("can create channel", async () => { test("can create channel", async () => {
state.channelName = "Test Channel"; state.channelName = "Test Channel";
state.channelDescription = "Test Channel Desc";
const response = await apiPost( const response = await apiPost(
`channel`, `channel`,
{ {
name: state.channelName, name: state.channelName,
description: state.channelDescription,
communityId: state.communityId, communityId: state.communityId,
}, },
state.token1, state.token1,
@ -205,18 +209,22 @@ test("can create channel", async () => {
assert.equal(validate(response.id), true); assert.equal(validate(response.id), true);
assert.equal(response.name, state.channelName); assert.equal(response.name, state.channelName);
assert.equal(response.description, state.channelDescription);
assert.equal(response.communityId, state.communityId); assert.equal(response.communityId, state.communityId);
assert.notEqual(response.creationDate, undefined);
state.channelId = response.id; state.channelId = response.id;
}); });
test("can create role", async () => { test("can create role", async () => {
state.roleName = "Test Role"; state.roleName = "Test Role";
state.roleDescription = "Test Role Desc";
const response = await apiPost( const response = await apiPost(
`role`, `role`,
{ {
name: state.roleName, name: state.roleName,
description: state.roleDescription,
communityId: state.communityId, communityId: state.communityId,
permissions: ["MEMBERS_READ", "CHANNELS_READ", "ROLES_READ"], permissions: ["MEMBERS_READ", "CHANNELS_READ", "ROLES_READ"],
}, },
@ -225,7 +233,10 @@ test("can create role", async () => {
assert.equal(validate(response.id), true); assert.equal(validate(response.id), true);
assert.equal(response.name, state.roleName); assert.equal(response.name, state.roleName);
assert.equal(response.description, state.roleDescription);
assert.equal(response.communityId, state.communityId); assert.equal(response.communityId, state.communityId);
assert.equal(response.permissions.length, 3);
assert.notEqual(response.creationDate, undefined);
state.roleId = response.id; state.roleId = response.id;
}); });
@ -253,7 +264,7 @@ test("can get members", async () => {
assert.equal(response.id, state.communityId); assert.equal(response.id, state.communityId);
assert.equal(response.name, state.communityName); assert.equal(response.name, state.communityName);
assert.equal(response.members.length === 2, true); assert.equal(response.members.length, 2);
}); });
test("can get channels", async () => { test("can get channels", async () => {
@ -264,7 +275,7 @@ test("can get channels", async () => {
assert.equal(response.id, state.communityId); assert.equal(response.id, state.communityId);
assert.equal(response.name, state.communityName); assert.equal(response.name, state.communityName);
assert.equal(response.channels.length === 1, true); assert.equal(response.channels.length, 1);
}); });
test("can get roles", async () => { test("can get roles", async () => {
@ -275,7 +286,7 @@ test("can get roles", async () => {
assert.equal(response.id, state.communityId); assert.equal(response.id, state.communityId);
assert.equal(response.name, state.communityName); assert.equal(response.name, state.communityName);
assert.equal(response.roles.length === 1, true); assert.equal(response.roles.length, 1);
}); });
test("can get permissions", async () => { test("can get permissions", async () => {
@ -283,8 +294,11 @@ test("can get permissions", async () => {
assert.equal(response.id, state.roleId); assert.equal(response.id, state.roleId);
assert.equal(response.name, state.roleName); assert.equal(response.name, state.roleName);
assert.equal(response.description, state.roleDescription);
assert.equal(response.communityId, state.communityId); 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 () => { test("can unassign role from user", async () => {