import { HttpCodes } from "../config/Errors";
import { UserD } from "../models/user.model";
import { RegistrationModel } from "../models/registration.model";
import registerLogs, {
registrationLogger,
IRegistrationLogs,
} from "../services/registration/registration.logs";
import { MyRequest } from "../types/Express";
import { Response } from "express";
import { formatString } from "../utils/Strings";
import { ErrorResponse, SuccessResponse } from "../utils/Response";
import { parseAsync } from "json2csv";
/**
* @description Register participant for the event
*/
export const register = async (req: MyRequest<UserD>, res: Response) => {
const { email, ...registrationData } = req.body as RegistrationI;
try {
const registrationExist = await RegistrationModel.findOne({
email,
});
if (registrationExist) {
const msg = formatString(
registerLogs.REGISTER_ERROR_EMAIL_EXIST.message,
{ email }
);
registrationLogger.error(msg);
return ErrorResponse(res, HttpCodes.BadRequest.code, msg);
}
console.log(registrationData);
const registration = new RegistrationModel({
email,
...registrationData,
});
const savedRegistration = await registration.save();
const msg = formatString(registerLogs.REGISTER_SUCCESS.message, { email });
registrationLogger.info(msg);
return SuccessResponse(res, HttpCodes.Created.code, savedRegistration, msg);
} catch (err) {
const msg = formatString(registerLogs.REGISTER_ERROR.message, {
error: (err as Error)?.message || "",
email,
});
registrationLogger.error(msg, err as Error);
return ErrorResponse(res, HttpCodes.InternalServerError.code, msg, err);
}
};
export const getRegistrations = async (
req: MyRequest<UserD>,
res: Response
) => {
try {
const registrations = await RegistrationModel.find();
const msg = formatString(
registerLogs.GET_ALL_REGISTRATIONS_SUCCESS.message,
{
count: registrations.length,
}
);
registrationLogger.info(msg);
return SuccessResponse(res, HttpCodes.OK.code, registrations, msg);
} catch (err) {
const msg = formatString(registerLogs.GET_ALL_REGISTRATIONS_ERROR.message, {
error: (err as Error)?.message || "",
});
registrationLogger.error(msg, err as Error);
return ErrorResponse(res, HttpCodes.InternalServerError.code, msg, err);
}
};
export const getRegistration = async (req: MyRequest<UserD>, res: Response) => {
const { id } = req.params;
try {
const registration = await RegistrationModel.findOne({ _id: id });
if (!registration) {
const msg = formatString(registerLogs.REGISTRATION_NOT_FOUND.message, {
id,
});
registrationLogger.error(msg);
return ErrorResponse(res, HttpCodes.NotFound.code, msg);
}
const msg = formatString(registerLogs.GET_REGISTRATION_SUCCESS.message, {
id,
});
registrationLogger.info(msg);
return SuccessResponse(res, HttpCodes.OK.code, registration, msg);
} catch (err) {
const msg = formatString(registerLogs.GET_REGISTRATION_ERROR.message, {
error: (err as Error)?.message || "",
id,
});
registrationLogger.error(msg, err as Error);
return ErrorResponse(res, HttpCodes.InternalServerError.code, msg, err);
}
};
export const exportRegistrations = async (
req: MyRequest<UserD>,
res: Response
) => {
try {
const registrations = await RegistrationModel.find();
if (!registrations.length) {
const msg = registerLogs.EXPORT_REGISTRATIONS_NOT_FOUND.message;
registrationLogger.error(msg);
return ErrorResponse(res, HttpCodes.NotFound.code, msg);
}
// declare enum YearOfStudy {
// L1 = "L1",
// L2 = "L2",
// L3 = "L3",
// M1 = "M1",
// M2 = "M2",
// N_A = "N/A"
// }
// declare interface RegistrationI {
// firstName: string;
// lastName: string;
// email: string;
// phone: string;
// proffession: string;
// university?: string;
// yearOfStudy?: YearOfStudy;
// nationalId?: string;
// fieldOfStudy?: string;
// skills: string[];
// github?: string;
// linkedin?: string;
// portfolio?: string;
// otherLink?: string;
// teamname?: string;
// participatedQuest: string;
// availableQuest: string;
// fullDurationQuest: string;
// motivationQuest: string;
// }
const registrationsFormated = registrations.map((registration) => {
return {
"First Name": registration.firstName,
"Last Name": registration.lastName,
Email: registration.email,
Phone: registration.phone,
Profession: registration.profession,
University: registration.university,
"Year of Study": registration.yearOfStudy,
"National ID": registration.nationalId,
"Profession Work": registration.otherprofession,
"Field of Study": registration.fieldOfStudy,
Skills: registration.skills.map((skill) => skill).join(" ; "),
Github: registration.github,
Linkedin: registration.linkedin,
Portfolio: registration.portfolio,
Discord: registration.discordUsername,
"Team name": registration.teamname,
"Have you participated in a hackathon before?":
registration.participatedQuest,
"Are you available to participate in any workshops or activities?":
registration.availableQuest,
"Are you able to commit to the full duration of the hackathon event?":
registration.fullDurationQuest,
"What is your motivation for participating in this hackathon?":
registration.motivationQuest,
};
});
const csv = await parseAsync(registrationsFormated);
res.setHeader("Content-Type", "text/csv");
res.setHeader(
"Content-Disposition",
"attachment; filename=mc-hack-registrations.csv"
);
const csvWithSeparator = csv.replace(/,/g, ";");
res.status(HttpCodes.OK.code).send(csvWithSeparator);
const msg = registerLogs.EXPORT_REGISTRATIONS_SUCCESS.message;
registrationLogger.info(msg);
} catch (err) {
const msg = formatString(registerLogs.EXPORT_REGISTRATIONS_ERROR.message, {
error: (err as Error)?.message || "",
});
registrationLogger.error(msg, err as Error);
return ErrorResponse(res, HttpCodes.InternalServerError.code, msg, err);
}
};