This commit is contained in:
Sebastian Rindom committed 2021-06-15 15:03:03 +02:00
1 parent 99eedb14fa
commit 27173d7e6a
4 files changed
+295 -24

No files matched your search

+65 -14
View File
@@ -8,11 +8,17 @@ import { countries } from "../utils/countries"
* @implements BaseService
*/
class RegionService extends BaseService {
static Events = {
UPDATED: "region.updated",
CREATED: "region.created",
}
constructor({
manager,
regionRepository,
countryRepository,
storeService,
eventBusService,
currencyRepository,
paymentProviderRepository,
fulfillmentProviderRepository,
@@ -33,6 +39,9 @@ class RegionService extends BaseService {
/** @private @const {StoreService} */
this.storeService_ = storeService
/** @private @const {EventBus} */
this.eventBus_ = eventBusService
/** @private @const {CurrencyRepository} */
this.currencyRepository_ = currencyRepository
@@ -60,6 +69,7 @@ class RegionService extends BaseService {
currencyRepository: this.currencyRepository_,
countryRepository: this.countryRepository_,
storeService: this.storeService_,
eventBusService: this.eventBus_,
paymentProviderRepository: this.paymentProviderRepository_,
paymentProviderService: this.paymentProviderService_,
fulfillmentProviderRepository: this.fulfillmentProviderRepository_,
@@ -117,6 +127,13 @@ class RegionService extends BaseService {
const created = regionRepository.create(regionObject)
const result = await regionRepository.save(created)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.CREATED, {
id: result.id,
})
return result
})
}
@@ -168,6 +185,14 @@ class RegionService extends BaseService {
}
const result = await regionRepository.save(region)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.UPDATED, {
id: result.id,
fields: Object.keys(update),
})
return result
})
}
@@ -390,6 +415,14 @@ class RegionService extends BaseService {
region.countries = [...(region.countries || []), country]
const updated = await regionRepo.save(region)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.UPDATED, {
id: updated.id,
fields: ["countries"],
})
return updated
})
}
@@ -419,6 +452,12 @@ class RegionService extends BaseService {
)
const updated = await regionRepo.save(region)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.UPDATED, {
id: updated.id,
fields: ["countries"],
})
return updated
})
}
@@ -458,6 +497,14 @@ class RegionService extends BaseService {
region.payment_providers = [...region.payment_providers, pp]
const updated = await regionRepo.save(region)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.UPDATED, {
id: updated.id,
fields: ["payment_providers"],
})
return updated
})
}
@@ -497,6 +544,12 @@ class RegionService extends BaseService {
region.fulfillment_providers = [...region.fulfillment_providers, fp]
const updated = await regionRepo.save(region)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.UPDATED, {
id: updated.id,
fields: ["fulfillment_providers"],
})
return updated
})
}
@@ -525,6 +578,12 @@ class RegionService extends BaseService {
)
const updated = await regionRepo.save(region)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.UPDATED, {
id: updated.id,
fields: ["payment_providers"],
})
return updated
})
}
@@ -553,23 +612,15 @@ class RegionService extends BaseService {
)
const updated = await regionRepo.save(region)
await this.eventBus_
.withTransaction(manager)
.emit(RegionService.Events.UPDATED, {
id: updated.id,
fields: ["fulfillment_providers"],
})
return updated
})
}
/**
* Decorates a region
* @param {object} region - the region to decorate
* @param {[string]} fields - the fields to include
* @param {[string]} expandFields - the fields to expand
* @return {Region} the region
*/
async decorate(region, fields, expandFields = []) {
const requiredFields = ["id", "metadata"]
const decorated = _.pick(region, fields.concat(requiredFields))
const final = await this.runDecorators_(decorated)
return final
}
}
export default RegionService