File: /home/ekspardev/tubisad-backend/src/app/demand/controller/demand.controller.js
const DonatedProjects = require("../../../core/model/donated-projects/donated_projects.model");
const WasteTypes = require("../../../core/model/waste-type/wasted_type.model");
const Demands = require("../model/demand.model");
const fs = require('fs');
exports.getAllDemands = async (req, res) => {
const demands = await Demands.findAll({
include: [
{
model: WasteTypes,
attributes: ['name']
},
{
model: DonatedProjects,
attributes: ['name', 'firstLogo', 'secondLogo']
}
]
});
if (demands.length == 0) {
return res.status(200).json({ success: true, status: true, message: 'There is no data!' });
}
return res.status(200).json({ success: true, status: true, data: demands });
};
exports.getDemandById = async (req, res) => {
const id = req.body.id;
const demand = await Demands.findAll({
where: { id: id },
include: [
{
model: WasteTypes,
attributes: ['name']
},
{
model: DonatedProjects,
attributes: ['name', 'firstLogo', 'secondLogo']
}
]
});
if (id) {
if (demand.length == 0) {
return res.status(200).json({ success: true, status: false, message: 'There is no data!' });
}
return res.status(200).json({ success: true, status: true, data: demand });
}
return res.status(404).json({ success: false, status: false, message: '404 Not Found!' });
};
exports.getDemandByPhone = async (req, res) => {
const phone = req.body.phone;
const demand = await Demands.findAll({
where: { donorPhone: phone },
include: [
{
model: WasteTypes,
attributes: ['name']
},
{
model: DonatedProjects,
attributes: ['name', 'firstLogo', 'secondLogo'],
}
],
});
if (phone) {
if (demand.length == 0) {
return res.status(200).json({ success: true, status: false, message: 'There is no data!' });
}
return res.status(200).json({ success: true, status: true, data: demand });
}
return res.status(404).json({ success: false, status: false, message: '404 Not Found!' });
}
exports.createDemand = async (req, res) => {
const {
wasteType,
wasteWeight,
city,
wasteDoc,
district,
street,
neighborhood,
outdoorNo,
doorNo,
locationLat,
locationLon,
donorFullName,
donorPhone,
donorEmail,
donatedProject,
textApproval,
kvkkApproval,
smsApproval
} = req.body;
if (req.file !== undefined) {
const wasteDoc = req.file.filename;
const demand = await Demands.create({
wasteType: wasteType,
wasteWeight: wasteWeight,
wasteDoc: wasteDoc,
city: city,
district: district,
street: street,
neighborhood: neighborhood,
outdoorNo: outdoorNo,
doorNo: doorNo,
locationLat: locationLat,
locationLon: locationLon,
donorFullName: donorFullName,
donorPhone: donorPhone,
donorEmail: donorEmail,
donatedProject: donatedProject,
textApproval: textApproval,
kvkkApproval: kvkkApproval,
smsApproval: smsApproval
});
return res.status(201).json({ success: true, status: true, demand: demand });
} else {
const demand = await Demands.create({
wasteType: wasteType,
wasteWeight: wasteWeight,
wasteDoc: wasteDoc,
city: city,
district: district,
street: street,
neighborhood: neighborhood,
outdoorNo: outdoorNo,
doorNo: doorNo,
locationLat: locationLat,
locationLon: locationLon,
donorFullName: donorFullName,
donorPhone: donorPhone,
donorEmail: donorEmail,
donatedProject: donatedProject,
textApproval: textApproval,
kvkkApproval: kvkkApproval,
smsApproval: smsApproval
});
return res.status(201).json({ success: true, status: true, demand: demand });
}
};
exports.updateDemand = async (req, res) => {
const id = req.body.id;
const kvkkApproval = req.body.kvkkApproval;
const textApproval = req.body.textApproval;
const smsApproval = req.body.smsApproval;
const updatedRecord = await Demands.update(
{ kvkkApproval, textApproval, smsApproval },
{ where: { id } }
);
return res.status(201).json({ success: true, status: true, data: updatedRecord });
};
exports.getWasteDoc = async (req, res) => {
const name = req.query.name;
const image = `<img src="${process.env.BASE_URL}waste-doc/${name}" alt="${name}">`;
return res.status(200).send(image);
};