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

export const GetOrderById = async (req: MyRequest<UserI>, res: Response) => {
  const { order_id } = req.params;
  const result = await OrderServices.getOrder(parseInt(order_id));
  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 GetOrders = async (req: MyRequest<UserI>, res: Response) => {
  const result = await OrderServices.getOrders();
  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 GetOrdersByStatus = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { status } = req.params;
  const result = await OrderServices.getOrdersByStatus(status);
  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 CreateOrder = async (req: MyRequest<UserI>, res: Response) => {
  const {
    course_id,
    pack_id,
    school_id = null,
    teacher_id = null,
    price,
    status = 'active',
    note = null,
  } = req.body;
  let result;
  if (school_id)
    result = await OrderServices.createSchoolOrder(
      course_id,
      pack_id,
      school_id,
      price,
      status,
      note
    );
  if (teacher_id)
    result = await OrderServices.createTeacherOrder(
      course_id,
      pack_id,
      teacher_id,
      price,
      status,
      note
    );

  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);

  // no result return error school_id or teacher_id is required
  if (!result) {
    return ErrorResponse(res, 400, 'school_id or teacher_id is required', {
      school_id,
      teacher_id,
    });
  }
};

export const DeleteOrder = async (req: MyRequest<UserI>, res: Response) => {
  const { order_id } = req.params;
  const result = await OrderServices.deleteOrder(parseInt(order_id));
  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 UpdateOrder = async (req: MyRequest<UserI>, res: Response) => {
//   const { order_id } = req.params;
//   const { course_id, pack_id, school_id, teacher_id, price, status, note } =
//     req.body;
//   const result = await OrderServices.updateOrder(
//     parseInt(order_id),
//     parseInt(course_id),
//     parseInt(pack_id),
//     school_id,
//     teacher_id,
//     price,
//     status,
//     note
//   );
//   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 UpdateOrderStatus = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { order_id } = req.params;
  const { status } = req.body;
  const result = await OrderServices.updateOrderStatus(
    parseInt(order_id),
    status
  );
  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 GetOrdersByInstructor = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { inst_designer_id } = req.params;
  const result = await OrderServices.getOrdersByInstructor(
    parseInt(inst_designer_id)
  );
  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 updateSchoolOrder = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  
  const { order_id } = req.params;
  const { course_id, pack_id, school_id, price, status, note } = req.body;
  const result = await OrderServices.updateSchoolOrder(
    parseInt(order_id),
    course_id,
    pack_id,
    school_id,
    price,
    status,
    note || null
  );
  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 updateTeacherOrder = async (
  req: MyRequest<UserI>,
  res: Response
) => {
  const { order_id } = req.params;
  const { course_id, pack_id, teacher_id, price, status, note } = req.body;
  const result = await OrderServices.updateTeacherOrder(
    parseInt(order_id),
    course_id,
    pack_id,
    teacher_id,
    price,
    status,
    note || null
  );
  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);
};