diff --git a/app/controllers/calculation.controller.js b/app/controllers/calculation.controller.js index 2496ac1..228f545 100644 --- a/app/controllers/calculation.controller.js +++ b/app/controllers/calculation.controller.js @@ -2,7 +2,7 @@ const db = require("../models"); const logger = require("../services/logger"); const IPICalculationService = require("../services/ipi_calculation_service"); const SubmissionAutoFillService = require("../services/auto_fill_missing_quarterly_submissions_service"); - +const CalculationLog = db.CalculationLog; // Database configuration const dbConfig = { @@ -287,6 +287,49 @@ exports.survey_auto_submit = async (req, res) => { } }; +exports.getCalculationLogsByYearMonth = async (req, res) => { + try { + const { year, month } = req.query; + + if (!year) { + return res.status(400).json({ + message: "Year is required", + }); + } + + if (!month) { + return res.status(400).json({ + message: "Month is required", + }); + } + + const logs = await CalculationLog.findAll({ + where: { + reference_year: year, + reference_month: month, + }, + order: [["started_at", "DESC"]], + }); + + if (!logs.length) { + return res.status(404).json({ + message: "No calculation logs found", + }); + } + + return res.status(200).json({ + message: "Calculation logs fetched successfully", + data: logs, + }); + } catch (err) { + logger.error(`Error getting calculation logs: ${err.message }`); + logger.error(`Stack trace: ${err.stack}`); + return res.status(500).json({ + message: "Server error", + }); + } +}; + diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index a6ac037..e2f460e 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -1081,7 +1081,6 @@ exports.forgotPasswordRequestOTP = async (req, res) => { }); const formattedSupportPhone = formatUAEPhoneNumber(process.env.SUPPORT_PHONE); - // const formattedOtp = otp.split("").join(" "); const placeHolderData = { username: user.name, verfication_code: otp, diff --git a/app/models/calculation_log.model.js b/app/models/calculation_log.model.js new file mode 100644 index 0000000..162e4b0 --- /dev/null +++ b/app/models/calculation_log.model.js @@ -0,0 +1,47 @@ +module.exports = (sequelize, DataTypes) => { + const CalculationLog = sequelize.define("calculation_log", { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + calculation_type: { + type: DataTypes.STRING, + allowNull: false, + }, + reference_year: { + type: DataTypes.INTEGER, + allowNull: false, + }, + reference_month: { + type: DataTypes.INTEGER, + allowNull: false, + }, + status: { + type: DataTypes.STRING, + allowNull: false, + }, + records_processed: { + type: DataTypes.INTEGER, + allowNull: true, + }, + error_message: { + type: DataTypes.TEXT, + allowNull: true, + }, + started_at: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + completed_at: { + type: DataTypes.DATE, + allowNull: true, + }, + }, { + tableName: "calculation_log", + timestamps: false, + }); + + return CalculationLog; +}; diff --git a/app/models/index.js b/app/models/index.js index 1868525..052aa67 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -33,6 +33,7 @@ db.ManufacturingIpi = require("./manufacturingIPI.model.js")(sequelize, DataType db.Isic2DigitIndices = require("./isic2DigitIndices.model.js")(sequelize, DataTypes); db.Isic3DigitIndices = require("./isic3DigitIndices.model.js")(sequelize, DataTypes); db.Isic4DigitIndices = require("./isic4DigitIndices.model.js")(sequelize, DataTypes); +db.CalculationLog = require("./calculation_log.model.js")(sequelize, DataTypes); // Associations db.Establishment.hasMany(db.EstablishmentUser, { foreignKey: "establishment_id", diff --git a/app/routes/routes.js b/app/routes/routes.js index a30d5c8..50c5b77 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -540,7 +540,6 @@ router.get("/testEmail", establishmentController.testEmail); * tags: [Establishments] * security: * - appSignature: [] - * - CSRF: [] * cookieAuth: [] # or bearerAuth: [] if you use Authorization header * requestBody: * required: true @@ -3220,6 +3219,42 @@ router.post("/auth/request-otp",[verifySignature], establishmentController.reque */ router.post("/auth/verify-otp",[verifySignature], establishmentController.verifyOTPForLogin); +/** + * @swagger + * /api/calculation-log/getCalculationLogs: + * get: + * summary: Get calculation logs by reference year and month + * tags: [CalculationLog] + * security: + * - appSignature: [] + * - CSRF: [] + * cookieAuth: [] + * parameters: + * - in: query + * name: year + * required: true + * schema: + * type: integer + * example: 2025 + * description: Reference year + * - in: query + * name: month + * required: true + * schema: + * type: integer + * example: 4 + * description: Reference month (1-12) + * responses: + * 200: + * description: Calculation log details fetched successfully + * 400: + * description: Year and month are required + * 404: + * description: No records found + * 500: + * description: Server error + */ +router.get( "/calculation-log/getCalculationLogs", [verifySignature, verifyToken], calculationController.getCalculationLogsByYearMonth);