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

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

View file

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

View file

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

View file

@ -3,9 +3,6 @@ import type {
IGetRoleParams,
IGetRoleResponseError,
IGetRoleResponseSuccess,
IGetRolePermissionsParams,
IGetRolePermissionsResponseError,
IGetRolePermissionsResponseSuccess,
IPostCreateRoleRequest,
IPostCreateRoleResponseError,
IPostCreateRoleResponseSuccess,
@ -63,38 +60,6 @@ const getRole = async (request: FastifyRequest, reply: FastifyReply) => {
} 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 createRoleRequest = request.body as IPostCreateRoleRequest;
const authHeader = request.headers["authorization"];
@ -228,7 +193,6 @@ const postUnassignRole = async (
export {
getRole,
getRolePermissions,
patchRole,
deleteRole,
postCreateRole,

View file

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

View file

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

View file

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