Implemented listing schemas.

This commit is contained in:
Tom Butcher 2026-03-21 21:39:21 +00:00
parent 61e4b666ef
commit 8a9c272de4
5 changed files with 133 additions and 0 deletions

View File

@ -45,6 +45,7 @@ productSchema.statics.recalculate = async function (product, user) {
for (const orderItem of draftOrderItems) { for (const orderItem of draftOrderItems) {
await orderItemModel.recalculate(orderItem, user); await orderItemModel.recalculate(orderItem, user);
} }
}; };
// Create and export the model // Create and export the model

View File

@ -39,6 +39,8 @@ import { invoiceModel } from './finance/invoice.schema.js';
import { clientModel } from './sales/client.schema.js'; import { clientModel } from './sales/client.schema.js';
import { salesOrderModel } from './sales/salesorder.schema.js'; import { salesOrderModel } from './sales/salesorder.schema.js';
import { marketplaceModel } from './sales/marketplace.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 // Map prefixes to models and id fields
export const models = { export const models = {
@ -323,4 +325,18 @@ export const models = {
label: 'Marketplace', label: 'Marketplace',
referenceField: '_reference', 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',
},
}; };

View 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);

View 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);

View File

@ -1,4 +1,5 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import { editObject } from '../../database.js';
import { generateId } from '../../utils.js'; import { generateId } from '../../utils.js';
const marketplaceSchema = new mongoose.Schema( const marketplaceSchema = new mongoose.Schema(
@ -11,6 +12,16 @@ const marketplaceSchema = new mongoose.Schema(
enum: ['ebay', 'etsy', 'tiktokShop'], enum: ['ebay', 'etsy', 'tiktokShop'],
}, },
active: { required: true, type: Boolean, default: true }, 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) // Provider-specific API configuration (flexible for eBay, Etsy, TikTok Shop)
config: { type: mongoose.Schema.Types.Mixed, default: {} }, config: { type: mongoose.Schema.Types.Mixed, default: {} },
}, },
@ -21,6 +32,26 @@ marketplaceSchema.virtual('id').get(function () {
return this._id; 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 }); marketplaceSchema.set('toJSON', { virtuals: true });
export const marketplaceModel = mongoose.model('marketplace', marketplaceSchema); export const marketplaceModel = mongoose.model('marketplace', marketplaceSchema);