Snai3i-MarketPlace / backend / src / services / pack / pack.service.ts
pack.service.ts
Raw
import packLogs, { IPackLogs, packLogger } from './pack.logs';
import { formatString } from '../../utils/Strings';
import { HttpCodes } from '../../config/Errors';
import { ErrorResponseC, SuccessResponseC } from '../services.response';
import { db } from '../../settings';
import { ResultSetHeader, RowDataPacket } from 'mysql2';

export class PackServices {
  /**
   * @description Create a pack
   * @param name - String
   * @param description - String
   * @param nb_teachers_accounts - Number
   * @param nb_students_accounts - Number
   * @param lms_access - String
   * @param additional_price - String
   * @param status - String
   * @param most_popular - String
   * @returns ResponseT
   */
  static createPack = async (
    name: string,
    description: string,
    nb_teachers_accounts: number,
    nb_students_accounts: number,
    lms_access: string,
    additional_price: string,
    status: string,
    most_popular: string
  ): Promise<ResponseT> => {
    try {
      const sqlInsertQuery =
        'INSERT INTO packs (name, description, nb_teachers_accounts, nb_students_accounts, lms_access, additional_price, status, most_popular) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';

      const result: any = await db.query<ResultSetHeader[]>(sqlInsertQuery, [
        name,
        description,
        nb_teachers_accounts,
        nb_students_accounts,
        lms_access,
        additional_price,
        status,
        most_popular,
      ]);

      const packId = result[0].insertId;

      const pack = {
        pack_id: packId,
        name,
        description,
        nb_teachers_accounts,
        nb_students_accounts,
        lms_access,
        additional_price,
        status,
        most_popular,
      };

      const resp: ICode<IPackLogs> = packLogs.CREATE_PACK_SUCCESS;
      const msg = formatString(resp.message, {
        packId,
      });
      packLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(resp.type, pack, msg, HttpCodes.Created.code);
    } catch (err) {
      const msg = formatString(packLogs.CREATE_PACK_ERROR.message, {
        error: (err as Error)?.message || '',
      });
      packLogger.error(msg, err as Error);
      return new ErrorResponseC(
        packLogs.CREATE_PACK_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Update a pack
   * @param packId - Number
   * @param name - String
   * @param description - String
   * @param nb_teachers_accounts - Number
   * @param nb_students_accounts - Number
   * @param lms_access - String
   * @param additional_price - String
   * @param status - String
   * @param most_popular - String
   * @returns ResponseT
   */
  static updatePack = async (
    packId: number,
    name: string,
    description: string,
    nb_teachers_accounts: number,
    nb_students_accounts: number,
    lms_access: string,
    additional_price: string,
    status: string,
    most_popular: string
  ): Promise<ResponseT> => {
    try {
      const sqlUpdateQuery =
        'UPDATE packs SET name = ?, description = ?, nb_teachers_accounts = ?, nb_students_accounts = ?, lms_access = ?, additional_price = ?, status = ?, most_popular = ? WHERE pack_id = ?';

      const result: any = await db.query<ResultSetHeader[]>(sqlUpdateQuery, [
        name,
        description,
        nb_teachers_accounts,
        nb_students_accounts,
        lms_access,
        additional_price,
        status,
        most_popular,
        packId,
      ]);

      const pack = {
        pack_id: packId,
        name,
        description,
        nb_teachers_accounts,
        nb_students_accounts,
        lms_access,
        additional_price,
        status,
        most_popular,
      };

      const resp: ICode<IPackLogs> = packLogs.UPDATE_PACK_SUCCESS;
      const msg = formatString(resp.message, {
        packId,
      });
      packLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        pack,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(packLogs.UPDATE_PACK_ERROR.message, {
        error: (err as Error)?.message || '',
      });
      packLogger.error(msg, err as Error);
      return new ErrorResponseC(
        packLogs.UPDATE_PACK_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Delete a pack
   * @param packId - Number
   * @returns ResponseT
   */
  static deletePack = async (packId: number): Promise<ResponseT> => {
    try {
      const sqlDeleteQuery = 'DELETE FROM packs WHERE pack_id = ?';
      await db.query<RowDataPacket[]>(sqlDeleteQuery, [packId]);

      const resp: ICode<IPackLogs> = packLogs.DELETE_PACK_SUCCESS;
      const msg = formatString(resp.message, {
        packId,
      });
      packLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        null,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(packLogs.DELETE_PACK_ERROR.message, {
        error: (err as Error)?.message || '',
      });
      packLogger.error(msg, err as Error);
      return new ErrorResponseC(
        packLogs.DELETE_PACK_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Get a pack
   * @param packId - Number
   * @returns ResponseT
   */
  static getPackById = async (packId: number): Promise<ResponseT> => {
    try {
      const sqlquery = 'SELECT * FROM packs WHERE pack_id = ?';
      const [[pack]] = await db.query<RowDataPacket[]>(sqlquery, [packId]);
      if (!pack) {
        const msg = formatString(packLogs.PACK_ERROR_NOT_FOUND.message, {
          packId,
        });
        packLogger.error(msg);
        return new ErrorResponseC(
          packLogs.PACK_ERROR_NOT_FOUND.type,
          HttpCodes.NotFound.code,
          msg
        );
      }

      const resp: ICode<IPackLogs> = packLogs.GET_PACK_SUCCESS;
      const msg = formatString(resp.message, {
        packId,
      });
      packLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        pack,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(packLogs.PACK_ERROR_GENERIC.message, {
        error: (err as Error)?.message || '',
      });
      packLogger.error(msg, err as Error);
      return new ErrorResponseC(
        packLogs.PACK_ERROR_GENERIC.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Get all packs
   * @returns ResponseT
   */
  static getPacks = async (): Promise<ResponseT> => {
    try {
      const sqlquery = 'SELECT * FROM packs';
      const [packs] = await db.query<RowDataPacket[]>(sqlquery);

      const resp: ICode<IPackLogs> = packLogs.GET_PACKS_SUCCESS;
      const msg = resp.message;
      packLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        packs,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(packLogs.PACK_ERROR_GENERIC.message, {
        error: (err as Error)?.message || '',
      });
      packLogger.error(msg, err as Error);
      return new ErrorResponseC(
        packLogs.PACK_ERROR_GENERIC.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Get all active packs
   * @returns ResponseT
   */
  static getActivePacks = async (): Promise<ResponseT> => {
    try {
      const sqlquery = 'SELECT * FROM packs WHERE status = ?';
      const [packs] = await db.query<RowDataPacket[]>(sqlquery, ['active']);

      const resp: ICode<IPackLogs> = packLogs.GET_PACKS_BY_STATUS_SUCCESS;
      const msg = resp.message;
      packLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        packs,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(packLogs.GET_PACKS_BY_STATUS_ERROR.message, {
        error: (err as Error)?.message || '',
      });
      packLogger.error(msg, err as Error);
      return new ErrorResponseC(
        packLogs.GET_PACKS_BY_STATUS_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Update pack status
   * @param packId - Number
   * @param status - String
   * @returns ResponseT
   */
  static updatePackStatus = async (
    packId: number,
    status: string
  ): Promise<ResponseT> => {
    try {
      const sqlUpdateQuery = 'UPDATE packs SET status = ? WHERE pack_id = ?';

      await db.query<ResultSetHeader[]>(sqlUpdateQuery, [status, packId]);

      const resp: ICode<IPackLogs> = packLogs.UPDATE_PACK_STATUS_SUCCESS;
      const msg = formatString(resp.message, {
        packId,
      });
      packLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        null,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(packLogs.UPDATE_PACK_STATUS_ERROR.message, {
        error: (err as Error)?.message || '',
      });
      packLogger.error(msg, err as Error);
      return new ErrorResponseC(
        packLogs.UPDATE_PACK_STATUS_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };
}