import { inject, injectable } from "inversify"; import Poi, { IPoi, IPoiResource, IPoiAttributes } from "../poi"; import { IJsonApiResource } from "../../interfaces"; import Mapper from "../../mapper"; import * as TYPES from "../../types"; import { poiType } from "./"; import { IPlaceResource } from "../place"; import { IImage, IImageAttributes, imageAssociationType, imageType } from "../image"; import imgix from "../../utils/imgix"; import { assignContainingPlaces, assignCity, assignContinent, assignCountry, assignNeighborhood, assignRegions } from "../../utils/placeUtilities"; @injectable() export class PoiMapper extends Mapper { @inject(TYPES.Bookmark) model: IPoi; toModel(resource: IPoiResource, included: IJsonApiResource[] = []): IPoi { const model = new Poi(); const attrs = resource.attributes; model.id = resource.id; model.address = attrs.address; model.emails = attrs.emails; model.hours = attrs.hours_string; model.location = attrs.location; model.name = attrs.name; model.alternative_names = attrs.alternative_names; model.poiAttributes = attrs.poi_attributes; model.poiType = attrs.poi_type; model.price = attrs.price_string; model.publishedAt = attrs.published_at; model.priceRange = attrs.price_range; model.review = attrs.review; model.richHoursString = attrs.rich_hours_string; model.lpInternalScore = attrs.score; model.searchableName = attrs.searchable_name; model.subtypes = attrs.subtypes; model.telephone = attrs.telephone; model.telephoneInfo = attrs.telephone_info; model.website = attrs.website; model.containingPlaceId = (resource.relationships["containing-place"].data).id; model.type = resource.type; const place: IPlaceResource = included.find((resource) => resource.id === model.containingPlaceId); if (place) { model.containingPlaceName = place.attributes.name; } if (place) { const ancestry = (place.relationships["ancestry"].data) || []; assignContainingPlaces(included, [place, ...ancestry], model); assignCity(included, ancestry, place, model); assignContinent(included, ancestry, place, model); assignCountry(included, ancestry, place, model); assignNeighborhood(included, ancestry, place, model); assignRegions(included, ancestry, place, model); } const associations = included.filter((resource) => resource.type === imageAssociationType && model.id === (resource.relationships.to.data).id ) .map((a) => (a.relationships.from.data).id); if (associations) { const images = included.filter((resource) => resource.type === imageType && associations.indexOf(resource.id) > -1, ); model.images = images.map((image: IJsonApiResource): IImage => ({ attribution: image.attributes.attribution, path: image.attributes.path, caption: image.attributes.caption, height: image.attributes.height, width: image.attributes.width, orientation: image.attributes.orientation, tags: image.attributes.tags || [], url: imgix(image.links.pixels, "media"), })); } return model; } toResource(model: IPoi): IPoiResource { const resource = { id: model.id, type: poiType, attributes: { address: model.address, emails: model.emails, hours_string: model.hours, location: model.location, name: model.name, poi_attributes: model.poiAttributes, poi_type: model.poiType, price_range: model.priceRange, price_string: model.price, published_at: model.publishedAt, review: model.review, rich_hours_string: model.richHoursString, score: model.lpInternalScore, searchable_name: model.searchableName, subtypes: model.subtypes, telephone: model.telephone, telephone_info: model.telephoneInfo, website: model.website, }, }; return resource; } } export default PoiMapper;