If Match Header in Ember app


#1

Hi guys, I’m quite new in Ember and I occured the problem i can’t handle. When I try to save my model, the following error occures: PATCH http://localhost:3000/api/v2/profile/properties/5f0b4492-a54a-4976-a81b-44c5a6755959/travel-agent-profiles/968d81c9-6cdb-4083-a861-d80054684a39 412 (Precondition Failed). I know it’s happening because I’m using 3 paths for backend, so my adapter looks like:

import ApplicationAdapter from “./application”; import Ember from “ember”;

export default ApplicationAdapter.extend({ session: Ember.inject.service(),

urlForFindRecord(id) { const host = this.get(“host”); const namespace = this.get(“namespace”); const propertyId = this.get(“session.selectedProperty.id”); return ${host}/${namespace}/profile/properties/${propertyId}/travel-agent-profiles/${id}; },

urlForCreateRecord() { const propertyId = this.get(“session.selectedProperty.id”); const host = this.get(“host”); const namespace = this.get(“namespace”); return ${host}/${namespace}/profile/properties/${propertyId}/travel-agent-profiles; },

urlForUpdateRecord(id) { const propertyId = this.get(“session.selectedProperty.id”); const host = this.get(“host”); return ${host}/api/v2/profile/properties/${propertyId}/travel-agent-profiles/${id}; },

buildURL(modelName, id, snapshot, requestType, query) { if(requestType === “POST”) { return this.urlForFindRecord(id).replace(“v1”, “v2”); } else { return this._super(modelName, id, snapshot, requestType, query); } },

methodForRequest(params) { let { requestType } = params;

switch (requestType) {
case "createRecord": return "POST";
case "updateRecord": return "PATCH";
case "deleteRecord": return "DELETE";
}

return "GET";

} });

And my serializer:

import ApplicationSerializer from “./application”; import DS from “ember-data”;

export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: { communicationDetails: { embedded: “always” } },

normalizeFindRecordResponse: function(store, primaryModelClass, payload, id, requestType) { const response = { id: payload.id, fullName: payload.details.fullName, isInactive: payload.isInactive, titleId: payload.details.titleId, abbreviation: payload.details.abbreviation, preferredLanguageCode: payload.details.preferedLanguageCode, iataCode: payload.details.iata, alternateFullName: payload.details.alternateFullName, addresses: payload.addresses.collection, communicationDetails: payload.communicationDetails.collection, aggregateVersion: payload.profileVersion, etag: payload[“etag”] };

return this._super(store, primaryModelClass, response, id, requestType);

},

serialize: function(snapshot) { const attributes = snapshot.attributes();

return {
  profile: {
    fullName: attributes.fullName,
    abbreviation: attributes.abbreviation,
    alternateFullName: attributes.alternateFullName,
    iata: attributes.iataCode,
    preferedLanguageCode: attributes.preferredLanguageCode
  },
  aggregateVersion: attributes.aggregateVersion
};

} });

And model:

import DS from “ember-data”; import ApplicationModel from “./application-model”; import HasCommunicationDetails from “./shared/has-communication-details”; import Ember from “ember”; import TravelAgentValidations from “above-cloud/validations/travel-agent-validations”; import { modelAction } from “ember-custom-actions”;

export default ApplicationModel.extend(HasCommunicationDetails, TravelAgentValidations, { fullName: DS.attr(), abbreviation: DS.attr(), isInactive: DS.attr(), alternateFullName: DS.attr(), iataCode: DS.attr(), primaryContacts: DS.attr(), addresses: DS.attr(), preferredLanguageCode: DS.attr(), aggregateVersion: DS.attr(), etag: DS.attr(),

activate: modelAction(“activate”, {type: “POST”}), deactivate: modelAction(“deactivate”, {type: “POST”}),

primaryAddress: function() { const addresses = this.get(“addresses”); return addresses.find(x => x.isPrimary); }.property(“addresses”),

formattedAddress: function() { const address = this.get(“primaryAddress”); return [ address.address, address.city, address.state, address.postCode, address.country ].filter(Ember.isPresent).join(", "); }.property(“primaryAddress”) });

The thing is that in backend it’s required to pass if match header whan I save the model. But in backend in api/v1 GET /api/v{apiVersion}/profile/properties/{propertyId}/travel-agent-profiles/{profileId} sends profileVersion, in api/v2 GET /api/v{apiVersion}/profile/properties/{propertyId}/travel-agent-profiles/{profileId} etag and in PATCH { “profile”: { “fullName”: “string”, “abbreviation”: “string”, “iata”: 0, “preferedLanguageCode”: “string”, “alternateFullName”: “string” } } does not send also etag as profileVersion. I know I have to pass if match header somehow. Any ideas what to do? Thank you!!