diff --git a/app/controllers/products.controller.js b/app/controllers/products.controller.js index 1553d94..9f084ce 100644 --- a/app/controllers/products.controller.js +++ b/app/controllers/products.controller.js @@ -3,7 +3,7 @@ const Product = db.Product; const fs = require("fs"); const csv = require("csv-parser"); const path = require("path"); - +const UnitMaster = db.UnitMaster; exports.createProduct = async (req, res) => { try { @@ -20,7 +20,15 @@ exports.createProduct = async (req, res) => { exports.getAllProducts = async (req, res) => { try { - const data = await Product.findAll(); + const data = await Product.findAll({ + include: [ + { + model: UnitMaster, + as: "unit", + attributes: ["uom"], + }, + ], + }); res.status(200).send({'status':"success",'message':"Fetched successfully",'data': data }); diff --git a/app/controllers/quarterlyWindowsConfiguration.controller.js b/app/controllers/quarterlyWindowsConfiguration.controller.js index 3522282..97a9587 100644 --- a/app/controllers/quarterlyWindowsConfiguration.controller.js +++ b/app/controllers/quarterlyWindowsConfiguration.controller.js @@ -1,4 +1,6 @@ const db = require("../models"); +const { Op } = require("sequelize"); +const { Sequelize } = require("sequelize"); const QuarterlyWindowsConfiguration = db.QuarterlyWindowsConfiguration; // Create new configuration @@ -19,6 +21,17 @@ exports.createConfig = async (req, res) => { exports.getAllConfigs = async (req, res) => { try { const data = await QuarterlyWindowsConfiguration.findAll({ + attributes: { + include: [ + [ + Sequelize.literal(`( + SELECT COUNT(*) FROM submission AS s + WHERE s.quarter = quarterly_windows_configuration_master.quarter AND s.year = quarterly_windows_configuration_master.year + )`), + "submission_count", + ], + ], + }, order: [["year", "DESC"]], }); res diff --git a/app/controllers/unitMasterController.js b/app/controllers/unitMasterController.js index 13c1de4..b43289c 100644 --- a/app/controllers/unitMasterController.js +++ b/app/controllers/unitMasterController.js @@ -1,4 +1,6 @@ -// controllers/unitMasterController.js +const db = require("../models"); +const { Op } = require("sequelize"); +const { Sequelize } = require("sequelize"); const { UnitMaster } = require("../models"); // Create new Unit @@ -14,13 +16,27 @@ exports.createUnit = async (req, res) => { // Get all Units exports.getAllUnits = async (req, res) => { try { - const units = await UnitMaster.findAll(); + const units = await UnitMaster.findAll({ + attributes: { + include: [ + [ + Sequelize.literal( + `(SELECT COUNT(*) FROM products AS P WHERE P.unit_id = UnitMaster.id)` + ), + "mapped_products_count", + ], + ], + }, + logging: console.log + }); + res.status(200).json({ status: "success", data: units }); } catch (err) { res.status(500).json({ status: "error", message: err.message }); } }; + // Get Unit by ID exports.getUnitById = async (req, res) => { try { diff --git a/app/models/product.model.js b/app/models/product.model.js index 1ab5b37..4275b30 100644 --- a/app/models/product.model.js +++ b/app/models/product.model.js @@ -17,6 +17,10 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.TEXT, allowNull: true, }, + unit_id: { + type: DataTypes.INTEGER, + allowNull: true, + }, is_active: { type: DataTypes.BOOLEAN, defaultValue: true, @@ -43,17 +47,22 @@ module.exports = (sequelize, DataTypes) => { }); Product.associate = (models) => { + Product.hasMany(models.EstablishmentProduct, { foreignKey: "product_id", as: "establishment_products", }); - }; - Product.associate = (models) => { Product.hasMany(models.SubmissionProduct, { foreignKey: "product_id", as: "submission_products", }); + + Product.belongsTo(models.UnitMaster, { + foreignKey: "unit_id", + as: "unit", + }); + }; return Product; diff --git a/app/models/quarterlyWindowsConfiguration.model.js b/app/models/quarterlyWindowsConfiguration.model.js index f9a8a33..7446d30 100644 --- a/app/models/quarterlyWindowsConfiguration.model.js +++ b/app/models/quarterlyWindowsConfiguration.model.js @@ -7,6 +7,10 @@ module.exports = (sequelize, DataTypes) => { autoIncrement: true, primaryKey: true, }, + survey_name: { + type: DataTypes.STRING(10), + allowNull: false, + }, year: { type: DataTypes.INTEGER, allowNull: false, diff --git a/app/routes/routes.js b/app/routes/routes.js index df050c7..c5097d6 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -917,6 +917,9 @@ router.delete("/establishment-users/:id",[verifySignature, verifyToken], establi * hs_description: * type: string * example: "Portable automatic data processing machines" + * unit_id: + * type: integer + * example: "select data from unit master" * is_active: * type: boolean * example: true @@ -987,10 +990,19 @@ router.get("/products/:id", [verifySignature, verifyToken],productController.get * properties: * product_name: * type: string - * example: "Desktop Computer" + * example: "Laptop" + * hs_code: + * type: string + * example: "8471.30" + * hs_description: + * type: string + * example: "Portable automatic data processing machines" + * unit_id: + * type: integer + * example: "select data from unit master" * is_active: * type: boolean - * example: false + * example: true * responses: * 200: * description: Product updated successfully @@ -2308,6 +2320,7 @@ router.put("/notification_templates/:id",[verifySignature, verifyToken], notific * schema: * type: object * properties: + * survey_name: { type: string, example: "Test"} * year: { type: integer } * quarter: { type: string, example: "Q1" } * start_date: { type: string, format: date } @@ -2373,6 +2386,14 @@ router.get("/quarterly_windows/:id",[verifySignature, verifyToken], quarterlyWin * application/json: * schema: * type: object + * properties: + * survey_name: { type: string, example: "Test"} + * year: { type: integer } + * quarter: { type: string, example: "Q1" } + * start_date: { type: string, format: date } + * end_date: { type: string, format: date } + * grace_periods_days: { type: integer } + * is_active: { type: boolean } * responses: * 200: * description: Updated successfully