@publidata/utils-data-manager
Version:
Collection of methods to extract data from publidata
263 lines (226 loc) • 6.4 kB
JavaScript
const {
getInfoFromMeta,
getIconFromWasteCollection,
getColorFromWasteCollection,
getCollectionMode,
getAcceptedWastes,
getRejectedWastes,
getGarbageTypes,
getWasteBlurb,
getObjectBlurb,
getCollectionModeIcon,
getGarbageTypesDetails,
getTranslationFromWastecollectionAndInstance
} = require("@publidata/utils-mapper");
const defaultIcons = require("../../../assets/default-icons.json");
const PublidataObjectService = require("../");
/**
* Class representing a waste collection service.
* @extends PublidataObjectService
*/
class PublidataObjectServiceWasteCollection extends PublidataObjectService {
/**
* @return {String}
*/
get blurb() {
const blurb = getObjectBlurb(this.object);
return this.memoize("blurb", blurb);
}
/**
* @return {String}
*/
get collectionMode() {
const collectionMode = getCollectionMode(this.object);
return this.memoize("collectionMode", collectionMode);
}
/**
* @return {Array}
*/
get acceptedWastes() {
const acceptedWastes = getAcceptedWastes(this.object);
return this.memoize("acceptedWaste", acceptedWastes);
}
/**
* @return {Array}
*/
get rejectedWastes() {
const rejectedWastes = getRejectedWastes(this.object);
return this.memoize("rejectedWaste", rejectedWastes);
}
/**
* @return {Array}
*/
get garbageTypes() {
const garbageTypes = getGarbageTypes(this.object);
return this.memoize("garbageTypes", garbageTypes);
}
get garbageTypesDetails() {
const garbageTypesDetails = getGarbageTypesDetails(
this.object,
this._source.metas,
this.instance.custom_settings
);
return this.memoize("garbageTypesDetails", garbageTypesDetails);
}
/**
* @return {String}
*/
get wasteBlurb() {
const wasteBlurb = getWasteBlurb(this.object);
return this.memoize("wasteBlurb", wasteBlurb);
}
/**
* @return {Array of Number}
*/
get facilityTypes() {
const facilityTypes = getInfoFromMeta(this.object, "facility_types") || [];
return this.memoize("facilityTypes", facilityTypes);
}
/**
* @return {Array}
*/
get procedures() {
const { garbageTypes, collectionMode } = this;
const { procedures } = this.object._source;
let filteredProcedures = procedures.filter(
item => item.name && (item.metas.value || item.metas.form_id)
); // Filter empty procedures used to override parent waste collection procedures
const isPublidataForm = metas => metas.form_id;
const isBookingForm = metas =>
garbageTypes[0] === "enc" &&
collectionMode === "booking" &&
metas.procedure_type === "Inscription à la collecte";
const disableGRC = String(this.instance.grc).toLowerCase() === "false";
const disableBookingForm =
String(this.instance.bookingForm).toLowerCase() === "false";
if (disableGRC && disableBookingForm)
// Reject all procedures managed by Publidata
filteredProcedures = procedures.filter(
({ metas }) => !isPublidataForm(metas)
);
else if (disableGRC)
// Reject all GRC procedures but keep booking forms
filteredProcedures = procedures.filter(
({ metas }) => !isPublidataForm(metas) || isBookingForm(metas)
);
else if (disableBookingForm)
// Reject all booking forms
filteredProcedures = procedures.filter(
({ metas }) => !isBookingForm(metas)
);
return this.memoize("procedures", filteredProcedures);
}
/**
* @return {Object} - If facility is not set yet, returns empty object
*/
// eslint-disable-next-line class-methods-use-this
get facility() {
return {};
}
/**
* @return {Object} - Parent information
*/
get parent() {
return this.memoize("parent", this._source.parent);
}
/**
* @return {Object} - metas informations
*/
get metas() {
return this.memoize("metas", this._source.metas);
}
/**
* @return {Array}
*/
get contacts() {
const { contacts } = this.object._source;
return this.memoize("contacts", contacts, true);
}
get collectionModeIcon() {
return this.memoize("collectionModeIcon", getCollectionModeIcon(this));
}
get icon() {
if (this.instance.icons) {
const icon = this.memoize(
"icon",
getIconFromWasteCollection(this, this.instance.icons),
true
);
if (icon) return icon;
}
return this.memoize(
"icon",
getIconFromWasteCollection(this, defaultIcons),
true
);
}
get color() {
return this.memoize("color", getColorFromWasteCollection(this));
}
get translation() {
if (this.instance.custom_settings) {
return getTranslationFromWastecollectionAndInstance(
this._source,
this.instance.custom_settings,
this.facility
);
}
return getTranslationFromWastecollectionAndInstance(
this._source,
this.instance,
this.facility
);
}
get serviceables() {
return this.memoize("serviceables", this._source.serviceables);
}
/**
* @return {Object} - service status information
*/
get serviceStatus() {
return this.memoize("serviceStatus", this._source.service_status);
}
get hasLinkedFacility() {
return this.memoize(
"hasLinkedFacility",
["recycling_bin", "recycling_center", "mobile"].indexOf(
getCollectionMode(this.object)
) >= 0
);
}
/**
* @return {Array} - Schedules
*/
get schedules() {
return this.memoize("schedules", this._source.schedules);
}
/**
* @return {Object} - Subscription details
*/
get subscription() {
return this.memoize("subscription", this.metas.subscription);
}
/**
* @param {Object} facility
* @return {PublidataObjectServiceCollect} Current collect with new property facility
*/
setFacility(facility) {
this.memoize("facility", facility, true);
return this;
}
setIsGatheringPointWasteCollection(boolean) {
this.memoize("isGatheringPointWasteCollection", boolean);
return this;
}
/**
* @description Create a new instance of PublidataObjectServiceWasteCollection
* @returns {PublidataObjectServiceWasteCollection}
*/
copy() {
return new PublidataObjectServiceWasteCollection(
this.object,
this.instance
);
}
}
module.exports = PublidataObjectServiceWasteCollection;