adAccount.js•2.36 kB
/**
* Ad Account model
* Stores information about Facebook ad accounts
*/
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const adAccountSchema = new Schema({
// Facebook ad account ID
accountId: {
type: String,
required: true,
unique: true,
index: true
},
// Ad account name
name: {
type: String,
required: true
},
// User who owns this ad account
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
index: true
},
// Account details
currency: {
type: String
},
timezone: {
type: String
},
// Account status
status: {
type: String,
enum: ['ACTIVE', 'DISABLED', 'UNSETTLED', 'PENDING_RISK_REVIEW', 'PENDING_SETTLEMENT', 'IN_GRACE_PERIOD', 'PENDING_CLOSURE', 'CLOSED', 'ANY_ACTIVE', 'ANY_CLOSED'],
default: 'ACTIVE'
},
// Account spending limits
spendCap: {
type: Number
},
balance: {
type: Number
},
// Account metadata
businessName: {
type: String
},
businessId: {
type: String
},
businessCity: {
type: String
},
businessCountryCode: {
type: String
},
// Account capabilities
capabilities: {
type: [String],
default: []
},
// Timestamps
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
},
// Last sync with Facebook
lastSyncedAt: {
type: Date,
default: Date.now
}
}, {
timestamps: true
});
// Indexes for faster queries
adAccountSchema.index({ accountId: 1 });
adAccountSchema.index({ userId: 1 });
adAccountSchema.index({ status: 1 });
adAccountSchema.index({ createdAt: -1 });
/**
* Find ad accounts by user ID
*/
adAccountSchema.statics.findByUserId = function(userId) {
return this.find({ userId });
};
/**
* Find active ad accounts
*/
adAccountSchema.statics.findActive = function() {
return this.find({ status: 'ACTIVE' });
};
/**
* Find ad account by Facebook account ID
*/
adAccountSchema.statics.findByAccountId = function(accountId) {
return this.findOne({ accountId });
};
/**
* Update last synced timestamp
*/
adAccountSchema.methods.updateLastSynced = function() {
this.lastSyncedAt = Date.now();
return this.save();
};
const AdAccount = mongoose.model('AdAccount', adAccountSchema);
module.exports = AdAccount;