All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
59 lines
1.7 KiB
JavaScript
59 lines
1.7 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import { editObject } from '../../database.js';
|
|
import { generateId } from '../../utils.js';
|
|
|
|
const marketplaceSchema = new mongoose.Schema(
|
|
{
|
|
_reference: { type: String, default: () => generateId()() },
|
|
name: { required: true, type: String },
|
|
provider: {
|
|
type: String,
|
|
required: true,
|
|
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: {} },
|
|
},
|
|
{ timestamps: true }
|
|
);
|
|
|
|
marketplaceSchema.index({ name: 'text', provider: 'text' });
|
|
|
|
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';
|
|
}
|
|
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);
|