Refactor part 1

This commit is contained in:
Aslan 2026-01-05 16:07:30 +01:00
parent c07d33bcc9
commit e21a807fb3
11 changed files with 71 additions and 146 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "tether", "name": "tether",
"version": "0.3.5", "version": "0.3.6",
"description": "Communication server using the Nexlink protocol", "description": "Communication server using the Nexlink protocol",
"repository": { "repository": {
"type": "git", "type": "git",

View file

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Channel" ADD COLUMN "description" TEXT;
-- AlterTable
ALTER TABLE "Role" ADD COLUMN "description" TEXT;

View file

@ -24,6 +24,7 @@ model Community {
model Channel { model Channel {
id String @id @unique @default(uuid()) id String @id @unique @default(uuid())
name String name String
description String?
community Community @relation(fields: [communityId], references: [id], onDelete: Cascade) community Community @relation(fields: [communityId], references: [id], onDelete: Cascade)
communityId String communityId String
creationDate DateTime @default(now()) creationDate DateTime @default(now())
@ -32,6 +33,7 @@ model Channel {
model Role { model Role {
id String @id @unique @default(uuid()) id String @id @unique @default(uuid())
name String name String
description String?
community Community @relation(fields: [communityId], references: [id], onDelete: Cascade) community Community @relation(fields: [communityId], references: [id], onDelete: Cascade)
communityId String communityId String
users User[] @relation(name: "UsersRolesToUsers") users User[] @relation(name: "UsersRolesToUsers")

View file

@ -45,6 +45,7 @@ const getChannel = async (request: FastifyRequest, reply: FastifyReply) => {
return { return {
id: channel.id, id: channel.id,
name: channel.name, name: channel.name,
description: channel.description,
communityId: channel.communityId, communityId: channel.communityId,
creationDate: channel.creationDate.getTime(), creationDate: channel.creationDate.getTime(),
} as IGetChannelResponseSuccess; } as IGetChannelResponseSuccess;
@ -68,7 +69,9 @@ const postCreateChannel = async (
return { return {
id: channel.id, id: channel.id,
name: channel.name, name: channel.name,
description: channel.description,
communityId: channel.communityId, communityId: channel.communityId,
creationDate: channel.creationDate.getTime(),
} as IPostCreateChannelResponseSuccess; } as IPostCreateChannelResponseSuccess;
}; };
@ -100,7 +103,9 @@ const patchChannel = async (request: FastifyRequest, reply: FastifyReply) => {
return { return {
id: channel.id, id: channel.id,
name: channel.name, name: channel.name,
description: channel.description,
communityId: channel.communityId, communityId: channel.communityId,
creationDate: channel.creationDate.getTime(),
} as IPatchChannelResponseSuccess; } as IPatchChannelResponseSuccess;
}; };

View file

@ -1,5 +1,13 @@
import type { API_ERROR } from "../errors.js"; import type { API_ERROR } from "../errors.js";
interface IChannel {
id: string;
name: string;
description: string;
communityId: string;
creationDate: number;
}
interface IGetChannelParams { interface IGetChannelParams {
id: string; id: string;
} }
@ -9,15 +17,11 @@ interface IGetChannelResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IGetChannelResponseSuccess { interface IGetChannelResponseSuccess extends IChannel {}
id: string;
name: string;
communityId: string;
creationDate: number;
}
interface IPostCreateChannelRequest { interface IPostCreateChannelRequest {
name: string; name: string;
description?: string;
communityId: string; communityId: string;
} }
@ -26,11 +30,7 @@ interface IPostCreateChannelResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostCreateChannelResponseSuccess { interface IPostCreateChannelResponseSuccess extends IChannel {}
id: string;
name: string;
communityId: string;
}
interface IPatchChannelParams { interface IPatchChannelParams {
id: string; id: string;
@ -38,6 +38,7 @@ interface IPatchChannelParams {
interface IPatchChannelRequest { interface IPatchChannelRequest {
name?: string; name?: string;
description?: string;
} }
interface IPatchChannelResponseError { interface IPatchChannelResponseError {
@ -45,11 +46,7 @@ interface IPatchChannelResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPatchChannelResponseSuccess { interface IPatchChannelResponseSuccess extends IChannel {}
id: string;
name: string;
communityId: string;
}
interface IDeleteChannelParams { interface IDeleteChannelParams {
id: string; id: string;
@ -66,6 +63,7 @@ interface IDeleteChannelResponseSuccess {
} }
export { export {
type IChannel,
type IGetChannelParams, type IGetChannelParams,
type IGetChannelResponseError, type IGetChannelResponseError,
type IGetChannelResponseSuccess, type IGetChannelResponseSuccess,

View file

@ -1,5 +1,13 @@
import type { API_ERROR } from "../errors.js"; import type { API_ERROR } from "../errors.js";
interface ICommunity {
id: string;
name: string;
description: string;
ownerId: string;
creationDate: number;
}
interface IGetCommunityParams { interface IGetCommunityParams {
id: string; id: string;
} }
@ -9,13 +17,7 @@ interface IGetCommunityResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IGetCommunityResponseSuccess { interface IGetCommunityResponseSuccess extends ICommunity {}
id: string;
name: string;
description: string;
ownerId: string;
creationDate: number;
}
interface IPostCreateCommunityRequest { interface IPostCreateCommunityRequest {
name: string; name: string;
@ -27,12 +29,7 @@ interface IPostCreateCommunityResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostCreateCommunityResponseSuccess { interface IPostCreateCommunityResponseSuccess extends ICommunity {}
id: string;
name: string;
description: string;
ownerId: string;
}
interface IPatchCommunityParams { interface IPatchCommunityParams {
id: string; id: string;
@ -48,11 +45,7 @@ interface IPatchCommunityResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPatchCommunityResponseSuccess { interface IPatchCommunityResponseSuccess extends ICommunity {}
id: string;
name: string;
description: string;
}
interface IDeleteCommunityParams { interface IDeleteCommunityParams {
id: string; id: string;
@ -147,6 +140,7 @@ interface IPostCreateInviteResponseSuccess {
} }
export { export {
type ICommunity,
type IGetCommunityParams, type IGetCommunityParams,
type IGetCommunityResponseError, type IGetCommunityResponseError,
type IGetCommunityResponseSuccess, type IGetCommunityResponseSuccess,

View file

@ -3,9 +3,6 @@ import type {
IGetRoleParams, IGetRoleParams,
IGetRoleResponseError, IGetRoleResponseError,
IGetRoleResponseSuccess, IGetRoleResponseSuccess,
IGetRolePermissionsParams,
IGetRolePermissionsResponseError,
IGetRolePermissionsResponseSuccess,
IPostCreateRoleRequest, IPostCreateRoleRequest,
IPostCreateRoleResponseError, IPostCreateRoleResponseError,
IPostCreateRoleResponseSuccess, IPostCreateRoleResponseSuccess,
@ -63,38 +60,6 @@ const getRole = async (request: FastifyRequest, reply: FastifyReply) => {
} as IGetRoleResponseSuccess; } as IGetRoleResponseSuccess;
}; };
const getRolePermissions = async (
request: FastifyRequest,
reply: FastifyReply,
) => {
const { id } = request.params as IGetRolePermissionsParams;
const authHeader = request.headers["authorization"];
const role = await getRoleByIdAuth(id, authHeader);
if (!role) {
reply.status(404);
return {
id: id,
error: API_ERROR.NOT_FOUND,
} as IGetRolePermissionsResponseError;
}
if (role === API_ERROR.ACCESS_DENIED) {
reply.status(403);
return {
id: id,
error: API_ERROR.ACCESS_DENIED,
} as IGetRolePermissionsResponseError;
}
return {
id: role.id,
name: role.name,
communityId: role.communityId,
permissions: role.permissions,
creationDate: role.creationDate.getTime(),
} as IGetRolePermissionsResponseSuccess;
};
const postCreateRole = async (request: FastifyRequest, reply: FastifyReply) => { const postCreateRole = async (request: FastifyRequest, reply: FastifyReply) => {
const createRoleRequest = request.body as IPostCreateRoleRequest; const createRoleRequest = request.body as IPostCreateRoleRequest;
const authHeader = request.headers["authorization"]; const authHeader = request.headers["authorization"];
@ -228,7 +193,6 @@ const postUnassignRole = async (
export { export {
getRole, getRole,
getRolePermissions,
patchRole, patchRole,
deleteRole, deleteRole,
postCreateRole, postCreateRole,

View file

@ -3,7 +3,6 @@ import * as controller from "./role.js";
const roleRoutes = async (fastify: FastifyInstance) => { const roleRoutes = async (fastify: FastifyInstance) => {
fastify.get(`/:id`, controller.getRole); fastify.get(`/:id`, controller.getRole);
fastify.get(`/:id/permissions`, controller.getRolePermissions);
fastify.post(`/`, controller.postCreateRole); fastify.post(`/`, controller.postCreateRole);
fastify.patch(`/:id`, controller.patchRole); fastify.patch(`/:id`, controller.patchRole);
fastify.delete(`/:id`, controller.deleteRole); fastify.delete(`/:id`, controller.deleteRole);

View file

@ -1,6 +1,15 @@
import type { PERMISSION } from "../../services/auth/permission.js"; import type { PERMISSION } from "../../services/auth/permission.js";
import type { API_ERROR } from "../errors.js"; import type { API_ERROR } from "../errors.js";
interface IRole {
id: string;
name: string;
description: string;
permissions: PERMISSION[];
communityId: string;
creationDate: number;
}
interface IGetRoleParams { interface IGetRoleParams {
id: string; id: string;
} }
@ -10,32 +19,11 @@ interface IGetRoleResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IGetRoleResponseSuccess { interface IGetRoleResponseSuccess extends IRole {}
id: string;
name: string;
communityId: string;
creationDate: number;
}
interface IGetRolePermissionsParams {
id: string;
}
interface IGetRolePermissionsResponseError {
id: string;
error: API_ERROR;
}
interface IGetRolePermissionsResponseSuccess {
id: string;
name: string;
communityId: string;
permissions: PERMISSION[];
creationDate: number;
}
interface IPostCreateRoleRequest { interface IPostCreateRoleRequest {
name: string; name: string;
description?: string;
communityId: string; communityId: string;
permissions: PERMISSION[]; permissions: PERMISSION[];
} }
@ -45,11 +33,7 @@ interface IPostCreateRoleResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostCreateRoleResponseSuccess { interface IPostCreateRoleResponseSuccess extends IRole {}
id: string;
name: string;
communityId: string;
}
interface IPatchRoleParams { interface IPatchRoleParams {
id: string; id: string;
@ -57,6 +41,7 @@ interface IPatchRoleParams {
interface IPatchRoleRequest { interface IPatchRoleRequest {
name?: string; name?: string;
description?: string;
} }
interface IPatchRoleResponseError { interface IPatchRoleResponseError {
@ -64,12 +49,7 @@ interface IPatchRoleResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPatchRoleResponseSuccess { interface IPatchRoleResponseSuccess extends IRole {}
id: string;
name: string;
communityId: string;
permissions: PERMISSION[];
}
interface IDeleteRoleParams { interface IDeleteRoleParams {
id: string; id: string;
@ -98,12 +78,7 @@ interface IPostAssignRoleResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostAssignRoleResponseSuccess { interface IPostAssignRoleResponseSuccess extends IRole {}
id: string;
name: string;
communityId: string;
userId: string;
}
interface IPostUnassignRoleParams { interface IPostUnassignRoleParams {
id: string; id: string;
@ -118,20 +93,13 @@ interface IPostUnassignRoleResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostUnassignRoleResponseSuccess { interface IPostUnassignRoleResponseSuccess extends IRole {}
id: string;
name: string;
communityId: string;
userId: string;
}
export { export {
type IRole,
type IGetRoleParams, type IGetRoleParams,
type IGetRoleResponseError, type IGetRoleResponseError,
type IGetRoleResponseSuccess, type IGetRoleResponseSuccess,
type IGetRolePermissionsParams,
type IGetRolePermissionsResponseError,
type IGetRolePermissionsResponseSuccess,
type IPostCreateRoleRequest, type IPostCreateRoleRequest,
type IPostCreateRoleResponseError, type IPostCreateRoleResponseError,
type IPostCreateRoleResponseSuccess, type IPostCreateRoleResponseSuccess,

View file

@ -1,5 +1,15 @@
import type { API_ERROR } from "../errors.js"; import type { API_ERROR } from "../errors.js";
interface IUser {
id: string;
username: string;
email: string;
description: string;
admin: boolean;
registerDate: number;
lastLogin: number;
}
interface IGetLoggedUserResponseError { interface IGetLoggedUserResponseError {
error: API_ERROR; error: API_ERROR;
} }
@ -17,15 +27,7 @@ interface IGetUserResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IGetUserResponseSuccess { interface IGetUserResponseSuccess extends IUser {}
id: string;
username: string;
email: string;
description: string;
admin: boolean;
registerDate: number;
lastLogin: number;
}
interface IPostCreateUserRequest { interface IPostCreateUserRequest {
username: string; username: string;
@ -40,13 +42,7 @@ interface IPostCreateUserResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPostCreateUserResponseSuccess { interface IPostCreateUserResponseSuccess extends IUser {}
id: string;
username: string;
email: string;
description: string;
admin: boolean;
}
interface IPatchUserParams { interface IPatchUserParams {
id: string; id: string;
@ -62,11 +58,7 @@ interface IPatchUserResponseError {
error: API_ERROR; error: API_ERROR;
} }
interface IPatchUserResponseSuccess { interface IPatchUserResponseSuccess extends IUser {}
id: string;
email: string;
description: string;
}
interface IDeleteUserParams { interface IDeleteUserParams {
id: string; id: string;
@ -121,6 +113,7 @@ interface IGetCommunitiesResponseCommunity {
} }
export { export {
type IUser,
type IGetLoggedUserResponseError, type IGetLoggedUserResponseError,
type IGetLoggedUserResponseSuccess, type IGetLoggedUserResponseSuccess,
type IGetUserParams, type IGetUserParams,

View file

@ -279,10 +279,7 @@ test("can get roles", async () => {
}); });
test("can get permissions", async () => { test("can get permissions", async () => {
const response = await apiGet( const response = await apiGet(`role/${state.roleId}`, state.token2);
`role/${state.roleId}/permissions`,
state.token2,
);
assert.equal(response.id, state.roleId); assert.equal(response.id, state.roleId);
assert.equal(response.name, state.roleName); assert.equal(response.name, state.roleName);