504fb1bfa1
no-issue This adds the concept of "Value Objects" to an Offers properties, allowing us to move validation out and ensure that an Offer will only ever have valid properties, without having to duplicate checks - or leave them to the persistent layer. This means we can fail early, as well as write unit tests for all of our validation.
33 lines
853 B
JavaScript
33 lines
853 B
JavaScript
class UniqueChecker {
|
|
/**
|
|
* @param {import('./OfferRepository')} repository
|
|
* @param {import('knex').Transaction} transaction
|
|
*/
|
|
constructor(repository, transaction) {
|
|
this.repository = repository;
|
|
this.options = {
|
|
transacting: transaction
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param {import('./domain/models/OfferCode')} code
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
async isUniqueCode(code) {
|
|
const exists = await this.repository.existsByCode(code.value, this.options);
|
|
return !exists;
|
|
}
|
|
|
|
/**
|
|
* @param {import('./domain/models/OfferName')} name
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
async isUniqueName(name) {
|
|
const exists = await this.repository.existsByName(name.value, this.options);
|
|
return !exists;
|
|
}
|
|
}
|
|
|
|
module.exports = UniqueChecker;
|