Snai3i-MarketPlace / backend / src / services / meeting / meeting.service.ts
meeting.service.ts
Raw
import meetingLogs, { IMeetingLogs, meetingLogger } from './meeting.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 MeetingServices {
  /**
   * @description Create a meeting
   * @param fullName - String
   * @param phone - String
   * @param email - String
   * @param cause - String
   * @param status - String
   * @param notes - String
   * @param course_id - Number
   * @param pack_id - Number
   * @returns ResponseT
   */
  static createMeeting = async (
    fullName: string,
    phone: string,
    email: string,
    cause: string | null = null,
    status: string = 'pending',
    course_id: number | null = null,
    pack_id: number | null = null,
    notes: string | null = null
  ): Promise<ResponseT> => {
    try {
      const createdAt = new Date();
      const updatedAt = createdAt;
      

      const sqlInsertQuery =
        'INSERT INTO meetings (fullName, phone, email, cause, status, notes, course_id, pack_id, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';

      const result: any = await db.query<ResultSetHeader[]>(sqlInsertQuery, [
        fullName,
        phone,
        email,
        cause,
        status,
        notes,
        course_id,
        pack_id,
        createdAt,
        updatedAt,
      ]);

      const meetingId = result[0].insertId;

      const meeting = {
        meeting_id: meetingId,
        fullName,
        phone,
        email,
        cause,
        status,
        notes,
        course_id,
        pack_id,
      };

      const resp: ICode<IMeetingLogs> = meetingLogs.CREATE_MEETING_SUCCESS;
      const msg = formatString(resp.message, {
        meetingId,
      });
      meetingLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        meeting,
        msg,
        HttpCodes.Created.code
      );
    } catch (err) {
      const msg = formatString(meetingLogs.CREATE_MEETING_ERROR.message, {
        error: (err as Error)?.message || '',
      });
      meetingLogger.error(msg, err as Error);
      return new ErrorResponseC(
        meetingLogs.CREATE_MEETING_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description update meeting status
   * @param meetingId - Number
   * @param status - String
   * @returns ResponseT
   */
  static updateMeetingStatus = async (
    meetingId: number,
    status: string
  ): Promise<ResponseT> => {
    try {
      const updatedAt = new Date();

      const sqlUpdateQuery =
        'UPDATE meetings SET status = ?, updatedAt = ? WHERE meeting_id = ?';

      await db.query<RowDataPacket[]>(sqlUpdateQuery, [
        status,
        updatedAt,
        meetingId,
      ]);

      const resp: ICode<IMeetingLogs> =
        meetingLogs.UPDATE_MEETING_STATUS_SUCCESS;
      const msg = formatString(resp.message, {
        meetingId,
      });
      meetingLogger.info(msg, { type: resp.type });

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

  /**
   * @description Get a meeting
   * @param meetingId - Number
   * @returns ResponseT
   */
  static getMeeting = async (meetingId: number): Promise<ResponseT> => {
    try {
      const sqlquery = 'SELECT * FROM meetings WHERE meeting_id = ?';
      const [meeting] = await db.query<RowDataPacket[]>(sqlquery, [meetingId]);

      if (meeting.length === 0) {
        const msg = formatString(meetingLogs.MEETING_ERROR_NOT_FOUND.message, {
          meetingId,
        });
        meetingLogger.error(msg);
        return new ErrorResponseC(
          meetingLogs.MEETING_ERROR_NOT_FOUND.type,
          HttpCodes.NotFound.code,
          msg
        );
      }

      const resp: ICode<IMeetingLogs> = meetingLogs.GET_MEETING_SUCCESS;
      const msg = formatString(resp.message, {
        meetingId,
      });
      meetingLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        meeting[0],
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(meetingLogs.MEETING_ERROR_GENERIC.message, {
        error: (err as Error)?.message || '',
      });
      meetingLogger.error(msg, err as Error);
      return new ErrorResponseC(
        meetingLogs.MEETING_ERROR_GENERIC.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Get all meetings
   * @returns ResponseT
   */
  static getMeetings = async (): Promise<ResponseT> => {
    try {
      // sort by createdAt
      const sqlquery = 'SELECT m.*, c.title as courseName , p.name as packName FROM meetings m JOIN courses c ON m.course_id = c.course_id JOIN packs p ON m.pack_id = p.pack_id ORDER BY m.createdAt DESC';
      // const sqlquery = 'SELECT m.*, c.title as courseName , p.name as packName FROM Meetings m JOIN Courses c ON m.course_id = c.course_id JOIN Packs p ON m.pack_id = p.pack_id';
      
      
      const [meetings] = await db.query<RowDataPacket[]>(sqlquery);

      const resp: ICode<IMeetingLogs> = meetingLogs.GET_MEETINGS_SUCCESS;
      const msg = resp.message;
      meetingLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        meetings,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(meetingLogs.GET_MEETINGS_ERROR.message, {
        error: (err as Error)?.message || '',
      });
      meetingLogger.error(msg, err as Error);
      return new ErrorResponseC(
        meetingLogs.GET_MEETINGS_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description Get all meetings by status
   * @param status - String
   * @returns ResponseT
   */
  static getMeetingsByStatus = async (status: string): Promise<ResponseT> => {
    try {
      const sqlquery = 'SELECT * FROM meetings WHERE status = ?';
      const [meetings] = await db.query<RowDataPacket[]>(sqlquery, [status]);

      const resp: ICode<IMeetingLogs> =
        meetingLogs.GET_MEETINGS_BY_STATUS_SUCCESS;
      const msg = resp.message;
      meetingLogger.info(msg, { type: resp.type });

      return new SuccessResponseC(
        resp.type,
        meetings,
        msg,
        HttpCodes.Accepted.code
      );
    } catch (err) {
      const msg = formatString(
        meetingLogs.GET_MEETINGS_BY_STATUS_ERROR.message,
        {
          error: (err as Error)?.message || '',
        }
      );
      meetingLogger.error(msg, err as Error);
      return new ErrorResponseC(
        meetingLogs.GET_MEETINGS_BY_STATUS_ERROR.type,
        HttpCodes.InternalServerError.code,
        msg
      );
    }
  };

  /**
   * @description delete a meeting
   * @param meetingId - Number
   * @returns ResponseT
   */
  static deleteMeeting = async (meetingId: number): Promise<ResponseT> => {
    try {
      const sqlDeleteQuery = 'DELETE FROM meetings WHERE meeting_id = ?';
      await db.query<RowDataPacket[]>(sqlDeleteQuery, [meetingId]);

      const resp: ICode<IMeetingLogs> = meetingLogs.DELETE_MEETING_SUCCESS;
      const msg = formatString(resp.message, {
        meetingId,
      });
      meetingLogger.info(msg, { type: resp.type });

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