Implemented listing schemas.
This commit is contained in:
parent
61e4b666ef
commit
8a9c272de4
@ -45,6 +45,7 @@ productSchema.statics.recalculate = async function (product, user) {
|
||||
for (const orderItem of draftOrderItems) {
|
||||
await orderItemModel.recalculate(orderItem, user);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Create and export the model
|
||||
|
||||
@ -39,6 +39,8 @@ import { invoiceModel } from './finance/invoice.schema.js';
|
||||
import { clientModel } from './sales/client.schema.js';
|
||||
import { salesOrderModel } from './sales/salesorder.schema.js';
|
||||
import { marketplaceModel } from './sales/marketplace.schema.js';
|
||||
import { listingModel } from './sales/listing.schema.js';
|
||||
import { listingVarientModel } from './sales/listingvarient.schema.js';
|
||||
|
||||
// Map prefixes to models and id fields
|
||||
export const models = {
|
||||
@ -323,4 +325,18 @@ export const models = {
|
||||
label: 'Marketplace',
|
||||
referenceField: '_reference',
|
||||
},
|
||||
LST: {
|
||||
model: listingModel,
|
||||
idField: '_id',
|
||||
type: 'listing',
|
||||
label: 'Listing',
|
||||
referenceField: '_reference',
|
||||
},
|
||||
LVR: {
|
||||
model: listingVarientModel,
|
||||
idField: '_id',
|
||||
type: 'listingVarient',
|
||||
label: 'Listing Varient',
|
||||
referenceField: '_reference',
|
||||
},
|
||||
};
|
||||
|
||||
42
src/database/schemas/sales/listing.schema.js
Normal file
42
src/database/schemas/sales/listing.schema.js
Normal file
@ -0,0 +1,42 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { generateId } from '../../utils.js';
|
||||
const { Schema } = mongoose;
|
||||
|
||||
const listingSchema = new Schema(
|
||||
{
|
||||
_reference: { type: String, default: () => generateId()() },
|
||||
product: { type: Schema.Types.ObjectId, ref: 'product', required: false },
|
||||
marketplace: { type: Schema.Types.ObjectId, ref: 'marketplace', required: true },
|
||||
title: { type: String, required: false },
|
||||
state: {
|
||||
type: {
|
||||
type: String,
|
||||
enum: ['draft', 'active', 'inactive', 'deleted', 'suspended', 'syncing'],
|
||||
default: 'draft',
|
||||
},
|
||||
message: { type: String, required: false },
|
||||
},
|
||||
url: { type: String, required: false },
|
||||
price: { type: Number, required: false },
|
||||
currency: { type: String, required: false },
|
||||
lastSyncedAt: { type: Date, required: false },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
listingSchema.virtual('id').get(function () {
|
||||
return this._id;
|
||||
});
|
||||
|
||||
listingSchema.set('toJSON', {
|
||||
virtuals: true,
|
||||
transform(doc, ret) {
|
||||
if (!ret.state && ret.status) {
|
||||
ret.state = { type: ret.status, message: null };
|
||||
}
|
||||
if (ret.status) delete ret.status;
|
||||
return ret;
|
||||
},
|
||||
});
|
||||
|
||||
export const listingModel = mongoose.model('listing', listingSchema);
|
||||
43
src/database/schemas/sales/listingvarient.schema.js
Normal file
43
src/database/schemas/sales/listingvarient.schema.js
Normal file
@ -0,0 +1,43 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { generateId } from '../../utils.js';
|
||||
const { Schema } = mongoose;
|
||||
|
||||
const listingVarientSchema = new Schema(
|
||||
{
|
||||
_reference: { type: String, default: () => generateId()() },
|
||||
listing: { type: Schema.Types.ObjectId, ref: 'listing', required: true },
|
||||
product: { type: Schema.Types.ObjectId, ref: 'product', required: false },
|
||||
productSku: { type: Schema.Types.ObjectId, ref: 'productSku', required: false },
|
||||
state: {
|
||||
type: {
|
||||
type: String,
|
||||
enum: ['draft', 'active', 'inactive', 'deleted', 'suspended', 'syncing'],
|
||||
default: 'draft',
|
||||
},
|
||||
message: { type: String, required: false },
|
||||
},
|
||||
price: { type: Number, required: false },
|
||||
currency: { type: String, required: false },
|
||||
priceTaxRate: { type: Schema.Types.ObjectId, ref: 'taxRate', required: false },
|
||||
priceWithTax: { type: Number, required: false },
|
||||
lastSyncedAt: { type: Date, required: false },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
listingVarientSchema.virtual('id').get(function () {
|
||||
return this._id;
|
||||
});
|
||||
|
||||
listingVarientSchema.set('toJSON', {
|
||||
virtuals: true,
|
||||
transform(doc, ret) {
|
||||
if (!ret.state && ret.status) {
|
||||
ret.state = { type: ret.status, message: null };
|
||||
}
|
||||
if (ret.status) delete ret.status;
|
||||
return ret;
|
||||
},
|
||||
});
|
||||
|
||||
export const listingVarientModel = mongoose.model('listingVarient', listingVarientSchema);
|
||||
@ -1,4 +1,5 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { editObject } from '../../database.js';
|
||||
import { generateId } from '../../utils.js';
|
||||
|
||||
const marketplaceSchema = new mongoose.Schema(
|
||||
@ -11,6 +12,16 @@ const marketplaceSchema = new mongoose.Schema(
|
||||
enum: ['ebay', 'etsy', 'tiktokShop'],
|
||||
},
|
||||
active: { required: true, type: Boolean, default: true },
|
||||
connected: { type: Boolean, required: true, default: false },
|
||||
connectedAt: { type: Date, required: false },
|
||||
state: {
|
||||
type: {
|
||||
type: String,
|
||||
enum: ['active', 'inactive', 'suspended', 'ready', 'offline', 'syncing'],
|
||||
default: 'offline',
|
||||
},
|
||||
message: { type: String, required: false },
|
||||
},
|
||||
// Provider-specific API configuration (flexible for eBay, Etsy, TikTok Shop)
|
||||
config: { type: mongoose.Schema.Types.Mixed, default: {} },
|
||||
},
|
||||
@ -21,6 +32,26 @@ marketplaceSchema.virtual('id').get(function () {
|
||||
return this._id;
|
||||
});
|
||||
|
||||
marketplaceSchema.statics.recalculate = async function (marketplace, user) {
|
||||
let stateType;
|
||||
if (marketplace.active === false) {
|
||||
stateType = 'inactive';
|
||||
} else if (marketplace.connected === false) {
|
||||
stateType = 'disconnected';
|
||||
} else {
|
||||
stateType = 'ready';
|
||||
}
|
||||
console.log('recalculating marketplace state', stateType);
|
||||
marketplace.state = { type: stateType };
|
||||
await editObject({
|
||||
model: this,
|
||||
id: marketplace._id,
|
||||
updateData: { state: { type: stateType } },
|
||||
user,
|
||||
recalculate: false,
|
||||
});
|
||||
};
|
||||
|
||||
marketplaceSchema.set('toJSON', { virtuals: true });
|
||||
|
||||
export const marketplaceModel = mongoose.model('marketplace', marketplaceSchema);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user