aggio query

This commit is contained in:
Surya Paolo
2023-12-12 15:42:41 +01:00
parent 5f3c8a65ea
commit a2bd4f6e97
4 changed files with 86 additions and 35 deletions

View File

@@ -24,15 +24,9 @@ const orderSchema = new Schema({
status: {
type: Number,
},
idProduct: {
type: String
},
idProducer: {
type: String
},
idStorehouse: {
type: String
},
idProduct: { type: Schema.Types.ObjectId, ref: 'Product' },
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
idStorehouse: { type: Schema.Types.ObjectId, ref: 'StoreHouse' },
price: {
type: Number
},
@@ -98,12 +92,10 @@ module.exports.findAllIdApp = async function (idapp) {
const query = [
{ $match: { idapp } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{
$lookup: {
from: 'products',
localField: 'myidProd',
localField: 'idProduct',
foreignField: '_id',
as: 'product'
}
@@ -111,7 +103,7 @@ module.exports.findAllIdApp = async function (idapp) {
{
$lookup: {
from: 'producers',
localField: 'myidProducer',
localField: 'idProducer',
foreignField: '_id',
as: 'producer'
}
@@ -148,16 +140,21 @@ module.exports.createOrder = async function (order) {
});
}
module.exports.updateStatusOrders = async function (arrOrders, status) {
for (const order of arrOrders) {
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: status });
}
}
module.exports.getTotalOrderById = async function (id) {
const query = [
{ $match: { _id: ObjectID(id) } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{ "$addFields": { "myidStore": { "$toObjectId": "$idStorehouse" } } },
{
$lookup: {
from: 'products',
localField: 'myidProd',
localField: 'idProduct',
foreignField: '_id',
as: 'product'
}
@@ -165,7 +162,7 @@ module.exports.getTotalOrderById = async function (id) {
{
$lookup: {
from: 'producers',
localField: 'myidProducer',
localField: 'idProducer',
foreignField: '_id',
as: 'producer'
}
@@ -173,7 +170,7 @@ module.exports.getTotalOrderById = async function (id) {
{
$lookup: {
from: 'storehouses',
localField: 'myidStore',
localField: 'idStorehouse',
foreignField: '_id',
as: 'storehouse'
}