RICADO Gen 4 API JS Client

RICADO Gen 4 API JS Client

source

Models/Packhouse/Site/PrePackSampleModel.js

/**
 * File Auto-Generated by the RICADO Gen 4 PHP API Project
 * 
 * Do Not Edit this File Manually!
 */

import BaseModel from '../../../Models/BaseModel';

/**
 * Model Class for a PrePack Sample
 * 
 * @class
 * @hideconstructor
 * @extends BaseModel
 */
class PrePackSampleModel extends BaseModel
{
    /**
     * PrePackSampleModel Constructor
     * 
     * @protected
     * @param {number} siteId The Site ID associated with this PrePack Sample
     */
    constructor(siteId)
    {
        super();
        
        /**
         * The PrePack Sample ID
         * 
         * @type {string}
         * @public
         */
        this.id = "";
        
        /**
         * The Packing Line ID this Sample is associated with
         * 
         * @type {string}
         * @public
         */
        this.packingLineId = "";
        
        /**
         * The Packrun ID this Sample is associated with
         * 
         * @type {string}
         * @public
         */
        this.packrunId = "";
        
        /**
         * When this Sample was Created
         * 
         * @type {Date}
         * @public
         */
        this.createdTimestamp = new Date();
        
        /**
         * The ID of the User who created this Pre-Pack Sample
         * 
         * @type {string}
         * @public
         */
        this.userId = "";
        
        /**
         * The Name of the User who created this Pre-Pack Sample
         * 
         * @type {string}
         * @public
         */
        this.userName = "";
        
        /**
         * An Array of Defects found in this Pre-Pack Sample
         * 
         * @type {Array<{id: string, name: string, fruitCount: number}>}
         * @public
         */
        this.sampleDefects = [];
        
        /**
         * The Total Number of Fruit Sampled in this Pre-Pack Sample
         * 
         * @type {number}
         * @public
         */
        this.totalFruitSampled = 0;
        
        /**
         * Whether the PrePack Sample has been deleted
         * 
         * @type {boolean}
         * @public
         */
        this.deleted = false;
        
        /**
         * When the PrePack Sample was last updated
         * 
         * @type {Date}
         * @public
         */
        this.updateTimestamp = new Date();
        
        /**
         * The Site ID associated with this PrePack Sample
         * 
         * @type {number}
         * @public
         */
        this.siteId = siteId;
    }

    /**
     * Create a new **PrePackSampleModel** from a JSON Object or JSON String
     * 
     * @static
     * @public
     * @param {Object<string, any>|string} json A JSON Object or JSON String
     * @param {number} siteId The Site ID associated with this PrePack Sample
     * @return {PrePackSampleModel}
     */
    static fromJSON(json, siteId)
    {
        let model = new PrePackSampleModel(siteId);
        
        /**
         * The JSON Object
         * 
         * @type {Object<string, any>}
         */
        let jsonObject = {};
        
        if(typeof json === 'string')
        {
            jsonObject = JSON.parse(json);
        }
        else if(typeof json === 'object')
        {
            jsonObject = json;
        }
        
        if('id' in jsonObject)
        {
            model.id = (function(){
                if(typeof jsonObject['id'] !== 'string')
                {
                    return String(jsonObject['id']);
                }
        
                return jsonObject['id'];
            }());
        }
        
        if('packingLineId' in jsonObject)
        {
            model.packingLineId = (function(){
                if(typeof jsonObject['packingLineId'] !== 'string')
                {
                    return String(jsonObject['packingLineId']);
                }
        
                return jsonObject['packingLineId'];
            }());
        }
        
        if('packrunId' in jsonObject)
        {
            model.packrunId = (function(){
                if(typeof jsonObject['packrunId'] !== 'string')
                {
                    return String(jsonObject['packrunId']);
                }
        
                return jsonObject['packrunId'];
            }());
        }
        
        if('createdTimestamp' in jsonObject)
        {
            model.createdTimestamp = (function(){
                if(typeof jsonObject['createdTimestamp'] !== 'string')
                {
                    return new Date(String(jsonObject['createdTimestamp']));
                }
        
                return new Date(jsonObject['createdTimestamp']);
            }());
        }
        
        if('userId' in jsonObject)
        {
            model.userId = (function(){
                if(typeof jsonObject['userId'] !== 'string')
                {
                    return String(jsonObject['userId']);
                }
        
                return jsonObject['userId'];
            }());
        }
        
        if('userName' in jsonObject)
        {
            model.userName = (function(){
                if(typeof jsonObject['userName'] !== 'string')
                {
                    return String(jsonObject['userName']);
                }
        
                return jsonObject['userName'];
            }());
        }
        
        if('sampleDefects' in jsonObject)
        {
            model.sampleDefects = (function(){
                if(Array.isArray(jsonObject['sampleDefects']) !== true)
                {
                    return [];
                }
        
                return jsonObject['sampleDefects'].map((sampleDefectsItem) => {
                    return (function(){
                        let sampleDefectsItemObject = {};
                        
                        if(typeof sampleDefectsItem === 'object' && 'id' in sampleDefectsItem)
                        {
                            sampleDefectsItemObject.id = (function(){
                                if(typeof sampleDefectsItem.id !== 'string')
                                {
                                    return String(sampleDefectsItem.id);
                                }
        
                                return sampleDefectsItem.id;
                            }());
                        }
                        else
                        {
                            sampleDefectsItemObject.id = "";
                        }
                        
                        if(typeof sampleDefectsItem === 'object' && 'name' in sampleDefectsItem)
                        {
                            sampleDefectsItemObject.name = (function(){
                                if(typeof sampleDefectsItem.name !== 'string')
                                {
                                    return String(sampleDefectsItem.name);
                                }
        
                                return sampleDefectsItem.name;
                            }());
                        }
                        else
                        {
                            sampleDefectsItemObject.name = "";
                        }
                        
                        if(typeof sampleDefectsItem === 'object' && 'fruitCount' in sampleDefectsItem)
                        {
                            sampleDefectsItemObject.fruitCount = (function(){
                                if(typeof sampleDefectsItem.fruitCount !== 'number')
                                {
                                    return Number.isInteger(Number(sampleDefectsItem.fruitCount)) ? Number(sampleDefectsItem.fruitCount) : Math.floor(Number(sampleDefectsItem.fruitCount));
                                }
        
                                return Number.isInteger(sampleDefectsItem.fruitCount) ? sampleDefectsItem.fruitCount : Math.floor(sampleDefectsItem.fruitCount);
                            }());
                        }
                        else
                        {
                            sampleDefectsItemObject.fruitCount = 0;
                        }
        
                        return sampleDefectsItemObject;
                    }());
                });
            }());
        }
        
        if('totalFruitSampled' in jsonObject)
        {
            model.totalFruitSampled = (function(){
                if(typeof jsonObject['totalFruitSampled'] !== 'number')
                {
                    return Number.isInteger(Number(jsonObject['totalFruitSampled'])) ? Number(jsonObject['totalFruitSampled']) : Math.floor(Number(jsonObject['totalFruitSampled']));
                }
        
                return Number.isInteger(jsonObject['totalFruitSampled']) ? jsonObject['totalFruitSampled'] : Math.floor(jsonObject['totalFruitSampled']);
            }());
        }
        
        if('deleted' in jsonObject)
        {
            model.deleted = (function(){
                if(typeof jsonObject['deleted'] !== 'boolean')
                {
                    return Boolean(jsonObject['deleted']);
                }
        
                return jsonObject['deleted'];
            }());
        }
        
        if('updateTimestamp' in jsonObject)
        {
            model.updateTimestamp = (function(){
                if(typeof jsonObject['updateTimestamp'] !== 'string')
                {
                    return new Date(String(jsonObject['updateTimestamp']));
                }
        
                return new Date(jsonObject['updateTimestamp']);
            }());
        }
        
        return model;
    }
}

export default PrePackSampleModel;