Snai3i-MarketPlace / backend / src / controllers / users.controller.ts
users.controller.ts
Raw
import { Response } from 'express';
import { MyRequest } from '../types/Express';
import { ErrorResponse, SuccessResponse } from '../utils/Response';
import { UsersServices } from '../services/users/users.services';
import {
  ErrorResponseC,
  SuccessResponseC,
} from '../services/services.response';

export const GetUsersBySuperAdmin = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const userId = req.user!.user_id;
  const result = await UsersServices.getUsersBySuperAdmin(res, userId);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetUsersByAdmin = async (req: MyRequest<UserI>, res: Response) => {
  // const userId = req.user!.user_id;
  const userId = Number(req.user!.user_id);
  const userRole = req.user!.role;

  const result =
    userRole === 'super_admin'
      ? await UsersServices.getUsersBySuperAdmin(res, userId)
      : await UsersServices.getUsersByAdmin(res, userId);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetUserById = async (req: MyRequest<UserI>, res: Response) => {
  const userId = Number(req.params.userId);
  const result = await UsersServices.getUserById(res, userId);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetTeacherById = async (req: MyRequest<UserI>, res: Response) => {
  const teacherId = Number(req.params.teacherId);
  const result = await UsersServices.getTeacherById(res, teacherId);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const UpdateUserById = async (req: MyRequest<UserI>, res: Response) => {
  const userId = Number(req.params.userId);
  const {
    email,
    firstName,
    lastName,
    role,
    phone = null,
    ...additionalInfo
  } = req.body;

  const result = await UsersServices.updateUserById(res, userId, {
    email,
    firstName,
    lastName,
    role,
    phone,
    additionalInfo,
  });
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const UpdateTeacherById = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const teacherId = Number(req.params.teacherId);
  const {
    email,
    firstName,
    lastName,
    role,
    phone = null,
    ...additionalInfo
  } = req.body;

  const result = await UsersServices.updateTeacherById(res, teacherId, {
    email,
    firstName,
    lastName,
    role,
    phone,
    additionalInfo,
  });
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const DeleteUserById = async (req: MyRequest<UserI>, res: Response) => {
  const userId = Number(req.params.userId);
  const result = await UsersServices.deleteUserById(res, userId);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const DeleteTeacherById = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const teacherId = Number(req.params.teacherId);
  const result = await UsersServices.deleteTeacherById(res, teacherId);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetTeachersBySchool = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const schoolId = Number(req.params.schoolId);
  const result = await UsersServices.getTeachersBySchool(res, schoolId);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const bulkDeleteUsers = async (req: MyRequest<UserI>, res: Response) => {
  const { userIds } = req.body;

  const result = await UsersServices.bulkDeleteUsers(res, userIds);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const bulkDeleteTeachers = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { teacherIds } = req.body;

  const result = await UsersServices.bulkDeleteTeachers(res, teacherIds);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetSchools = async (req: MyRequest<UserI>, res: Response) => {
  const result = await UsersServices.getSchools(res);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetTeachers = async (req: MyRequest<UserI>, res: Response) => {
  const result = await UsersServices.getTeachers(res);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetInstDesigners = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const result = await UsersServices.getInstDesigners(res);
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};

export const GetStatistics = async (req: MyRequest<UserI>, res: Response) => {
  const result = await UsersServices.getStatistics();
  if (result instanceof SuccessResponseC)
    return SuccessResponse(
      res,
      result.code,
      result.data,
      result.message,
      result.status
    );
  if (result instanceof ErrorResponseC)
    return ErrorResponse(res, result.code, result.message, result.error);
};