import { inject, injectable } from "inversify"; import User, { IUser, IUserResponse, IUserResource, IUserAttributes, ISocialAccountResource, ISocialAccount, ICurrentLocation } from "../user"; import { IResourceMapper, IJsonApiResponse, IJsonApiResource } from "../../interfaces"; import * as TYPES from "../../types"; import Mapper from "../../mapper"; @injectable() export class UserMapper extends Mapper { @inject(TYPES.User) model: IUser; toModel(resource: IUserResource, included: IJsonApiResource[] = []): IUser { const model = new User(); model.id = resource.id; model.website = resource.attributes.website; model.username = resource.attributes.username; model.name = resource.attributes.name; model.updatedAt = resource.attributes.updated_at; model.createdAt = resource.attributes.created_at; model.nextTrip = resource.attributes.next_trip; model.nationality = resource.attributes.nationality; model.lastTrip = resource.attributes.last_trip; model.lastName = resource.attributes.last_name; model.languages = resource.attributes.languages; model.gender = resource.attributes.gender; model.firstName = resource.attributes.first_name; model.emailVerified = resource.attributes.email_verified; model.avatar = resource.attributes.avatar; model.aboutMe = resource.attributes.about_me; model.email = resource.attributes.email; model.unconfirmedEmail = resource.attributes.unconfirmed_email; model.socialAccounts = included .filter((a) => a.type === "social-account") .map((a: ISocialAccountResource) => { const account: ISocialAccount = { id: a.id, name: a.attributes.social_network, }; return account; }); let currentLocation = included .find((a) => a.type === "place"); if (currentLocation && currentLocation.attributes) { model.currentLocation = { id: currentLocation.id, name: currentLocation.attributes.name, place_type: currentLocation.attributes.place_type, }; } return model; } toResource(model: IUser): IUserResource { const resource = { id: model.id, type: "user", attributes: { website: model.website, username: model.username, name: model.name, updated_at: model.updatedAt, created_at: model.createdAt, next_trip: model.nextTrip, nationality: model.nationality, last_trip: model.lastTrip, last_name: model.lastName, languages: model.languages, gender: model.gender, first_name: model.firstName, email_verified: model.emailVerified, avatar: model.avatar, about_me: model.aboutMe, email: model.email, unconfirmed_email: model.unconfirmedEmail, ask_me_about: model.aboutMe, }, }; return resource; } } export default UserMapper;