p1 feedback : GWM
This commit is contained in:
parent
0688d95592
commit
710a290a68
@ -2,10 +2,31 @@ const db = require("../models");
|
|||||||
const { Op } = require("sequelize");
|
const { Op } = require("sequelize");
|
||||||
const { Sequelize } = require("sequelize");
|
const { Sequelize } = require("sequelize");
|
||||||
const QuarterlyWindowsConfiguration = db.QuarterlyWindowsConfiguration;
|
const QuarterlyWindowsConfiguration = db.QuarterlyWindowsConfiguration;
|
||||||
|
const Establishment = db.Establishment;
|
||||||
|
|
||||||
// Create new configuration
|
// Create new configuration
|
||||||
exports.createConfig = async (req, res) => {
|
exports.createConfig = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
const { quarter, year } = req.body;
|
||||||
|
|
||||||
|
// find config
|
||||||
|
const exists = await QuarterlyWindowsConfiguration.findOne({where: { quarter, year }});
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
return res.status(400).json({status: "failed", message: `Window already exists quarter: ${quarter}, year: ${year}`});
|
||||||
|
}
|
||||||
|
|
||||||
|
// get active establishment count
|
||||||
|
const assignedCount = await Establishment.count({
|
||||||
|
where: { is_active: true }
|
||||||
|
});
|
||||||
|
|
||||||
|
// set auto values before inserting
|
||||||
|
req.body.assigned = assignedCount;
|
||||||
|
req.body.responded = 0;
|
||||||
|
req.body.not_responded = assignedCount;
|
||||||
|
|
||||||
const data = await QuarterlyWindowsConfiguration.create(req.body);
|
const data = await QuarterlyWindowsConfiguration.create(req.body);
|
||||||
res.status(201).send({
|
res.status(201).send({
|
||||||
status: "success",
|
status: "success",
|
||||||
|
|||||||
@ -10,6 +10,7 @@ const ZeroTargetReasonMaster = db.ZeroTargetReasonMaster;
|
|||||||
const Product = db.Product;
|
const Product = db.Product;
|
||||||
const CityTown = db.CityTown;
|
const CityTown = db.CityTown;
|
||||||
const Emirate = db.Emirate;
|
const Emirate = db.Emirate;
|
||||||
|
const QuarterlyWindowsConfiguration = db.QuarterlyWindowsConfiguration;
|
||||||
const ExcelJS = require("exceljs");
|
const ExcelJS = require("exceljs");
|
||||||
const { Op } = require("sequelize");
|
const { Op } = require("sequelize");
|
||||||
const { Sequelize } = require("sequelize");
|
const { Sequelize } = require("sequelize");
|
||||||
@ -21,12 +22,33 @@ const { getQuarterPeriods } = require("../services/quarterService");
|
|||||||
exports.createSubmission = async (req, res) => {
|
exports.createSubmission = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const { products = [], ...submissionData } = req.body;
|
const { products = [], establishment_id, quarter, year, ...submissionData } = req.body;
|
||||||
|
|
||||||
if (products.length > 10)
|
if (products.length > 10)
|
||||||
return res.status(400).json({ status: "failed", message: "Max 10 products allowed" });
|
return res.status(400).json({ status: "failed", message: "Max 10 products allowed" });
|
||||||
|
|
||||||
const submission = await Submission.create(submissionData);
|
// check duplicate submission
|
||||||
|
const exists = await Submission.findOne({ where: {establishment_id,quarter,year} });
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
return res.status(400).json({status: "failed", message: `Submission already exists for establishment_id: ${establishment_id}, quarter: ${quarter}, year: ${year}`});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const submission = await Submission.create({establishment_id, quarter, year, ...submissionData});
|
||||||
|
|
||||||
|
// find config
|
||||||
|
const config = await QuarterlyWindowsConfiguration.findOne({where: { quarter, year }});
|
||||||
|
if (config) {
|
||||||
|
const currentResponded = config.responded ?? 0;
|
||||||
|
const currentNotResponded = config.not_responded ?? 0;
|
||||||
|
// respond increase, not responded decrease
|
||||||
|
await QuarterlyWindowsConfiguration.update({responded: currentResponded + 1, not_responded: currentNotResponded - 1 }, {where: { id: config.id }} );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (products.length) {
|
if (products.length) {
|
||||||
// products.forEach((p) => (p.submission_id = submission.id));
|
// products.forEach((p) => (p.submission_id = submission.id));
|
||||||
// await SubmissionProduct.bulkCreate(products);
|
// await SubmissionProduct.bulkCreate(products);
|
||||||
|
|||||||
@ -51,6 +51,9 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
|
assigned: { type: DataTypes.INTEGER },
|
||||||
|
responded: { type: DataTypes.INTEGER },
|
||||||
|
not_responded: { type: DataTypes.INTEGER },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tableName: "quarterly_windows_configuration_master",
|
tableName: "quarterly_windows_configuration_master",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user