From ceb71ecde2d72b66810af7fa4f9de2a922c0a420 Mon Sep 17 00:00:00 2001
From: Sergei Solo <olosegres@gmail.com>
Date: Sat, 11 Jul 2020 18:18:59 +0500
Subject: [PATCH] Adds related resource meta field processing

---
 src/JsonaTypes.ts                |  5 +++++
 src/builders/JsonDeserializer.ts | 15 +++++++++++----
 src/simplePropertyMappers.ts     |  6 +++++-
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/JsonaTypes.ts b/src/JsonaTypes.ts
index 6d9db47..e055341 100644
--- a/src/JsonaTypes.ts
+++ b/src/JsonaTypes.ts
@@ -12,6 +12,7 @@ export interface IJsonPropertiesMapper {
     setAttributes(model: TJsonaModel, attributes: TAnyKeyValueObject): void;
     setMeta(model: TJsonaModel, meta: TAnyKeyValueObject): void;
     setLinks(model: TJsonaModel, links: TAnyKeyValueObject): void;
+    setRelationMeta(model: TJsonaModel, relationMeta: TRelationMeta): void;
     setRelationships(model: TJsonaModel, relationships: TJsonaRelationships): void;
     setRelationshipLinks(parentModel: TJsonaModel, relationName: string, links: TJsonApiLinks): void;
     setRelationshipMeta(parentModel: TJsonaModel, relationName: string, meta: TAnyKeyValueObject): void;
@@ -146,6 +147,10 @@ export type TJsonaModel = {
     [propertyName: string]: any
 };
 
+export type TRelationMeta = {
+    [propertyName: string]: any
+};
+
 export type TJsonaRelationshipBuild = () => (TJsonaModel | Array<TJsonaModel>);
 export type TJsonaRelationships = {
     [relationName: string]: TJsonaRelationshipBuild | TJsonaModel | Array<TJsonaModel>
diff --git a/src/builders/JsonDeserializer.ts b/src/builders/JsonDeserializer.ts
index 9eddc34..8227223 100644
--- a/src/builders/JsonDeserializer.ts
+++ b/src/builders/JsonDeserializer.ts
@@ -5,7 +5,7 @@ import {
     TJsonApiBody,
     TJsonApiData,
     IJsonaDeserializer,
-    IDeserializeCache,
+    IDeserializeCache, TRelationMeta,
 } from '../JsonaTypes';
 
 export class JsonDeserializer implements IJsonaDeserializer {
@@ -66,7 +66,7 @@ export class JsonDeserializer implements IJsonaDeserializer {
         return stuff;
     }
 
-    buildModelByData(data: TJsonApiData): TJsonaModel {
+    buildModelByData(data: TJsonApiData, payload: TRelationMeta = {}): TJsonaModel {
         const cachedModel = this.dc.getCachedModel(data);
 
         if (cachedModel) {
@@ -92,6 +92,10 @@ export class JsonDeserializer implements IJsonaDeserializer {
                 this.pm.setLinks(model, data.links);
             }
 
+            if (payload.relationMeta) {
+                this.pm.setRelationMeta(model, payload.relationMeta);
+            }
+
             const relationships: null | TJsonaRelationships = this.buildRelationsByData(data, model);
 
             if (relationships) {
@@ -108,6 +112,9 @@ export class JsonDeserializer implements IJsonaDeserializer {
         if (data.relationships) {
             for (let k in data.relationships) {
                 const relation = data.relationships[k];
+                const relationPayload = {
+                    relationMeta: relation.meta,
+                };
 
                 if (Array.isArray(relation.data)) {
                     readyRelations[k] = [];
@@ -127,12 +134,12 @@ export class JsonDeserializer implements IJsonaDeserializer {
                             relationItem.type
                         );
                         readyRelations[k].push(
-                            this.buildModelByData(dataItem)
+                            this.buildModelByData(dataItem, relationPayload)
                         );
                     }
                 } else if (relation.data) {
                     let dataItem = this.buildDataFromIncludedOrData(relation.data.id, relation.data.type);
-                    readyRelations[k] = this.buildModelByData(dataItem);
+                    readyRelations[k] = this.buildModelByData(dataItem, relationPayload);
                 } else if (relation.data === null) {
                     readyRelations[k] = null;
                 }
diff --git a/src/simplePropertyMappers.ts b/src/simplePropertyMappers.ts
index 3f431cb..931e87d 100644
--- a/src/simplePropertyMappers.ts
+++ b/src/simplePropertyMappers.ts
@@ -3,7 +3,7 @@ import {
     IJsonPropertiesMapper,
     TAnyKeyValueObject,
     TJsonaModel,
-    TJsonaRelationships, TJsonaRelationshipBuild, TJsonApiLinks
+    TJsonaRelationships, TJsonaRelationshipBuild, TJsonApiLinks, TRelationMeta
 } from './JsonaTypes';
 
 export const RELATIONSHIP_NAMES_PROP = 'relationshipNames';
@@ -115,6 +115,10 @@ export class JsonPropertiesMapper implements IJsonPropertiesMapper {
         model.links = links;
     }
 
+    setRelationMeta(model: TJsonaModel, relationMeta: TRelationMeta) {
+        model.relationMeta = relationMeta;
+    }
+
     setRelationships(model: TJsonaModel, relationships: TJsonaRelationships) {
 
         Object.keys(relationships).forEach((propName) => {
