Ghost/ghost/offers/lib/domain/models/OfferCreatedAt.js
Jono M 3f70cc08b7
Fixed create offer API not returning created_at (#19143)
refs https://github.com/TryGhost/Product/issues/4196

The offers API basically returns the data you pass to it, rather than
the created database record. It looks like this is how it was intended
to work in the first place; the `setMilliseconds` is because the test
helper expects `.000Z`, which I assume is because MySQL will strip off
the milliseconds when it's saved.
2023-11-28 15:11:35 +00:00

31 lines
881 B
JavaScript

const ValueObject = require('./shared/ValueObject');
const InvalidOfferCreatedAt = require('../errors').InvalidOfferCreatedAt;
/** @extends ValueObject<string> */
class OfferCreatedAt extends ValueObject {
/** @param {Date} createdAt */
constructor(createdAt) {
super(createdAt.toISOString()); // Convert Date to ISO string
}
static create(createdAt) {
if (createdAt === null || createdAt === undefined) {
const now = new Date();
now.setMilliseconds(0);
return now.toISOString();
}
if (!(createdAt instanceof Date)) {
throw new InvalidOfferCreatedAt({
message: 'Offer `created_at` must be a Date.'
});
}
return createdAt.toISOString();
}
static InvalidOfferCreatedAt = InvalidOfferCreatedAt;
}
module.exports = OfferCreatedAt;