definma-ui/src/app/models/sample.model.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-07-30 14:23:51 +02:00
import pick from 'lodash/pick';
import cloneDeep from 'lodash/cloneDeep';
import {IdModel} from './id.model';
import {MaterialModel} from './material.model';
import {MeasurementModel} from './measurement.model';
import {BaseModel} from './base.model';
export class SampleModel extends BaseModel {
_id: IdModel = null;
color = '';
number = '';
type = '';
batch = '';
condition: {condition_template: string, [prop: string]: string} = {condition_template: null};
material_id: IdModel = null;
material: MaterialModel;
2020-06-26 11:09:59 +02:00
measurements: MeasurementModel[] = [];
note_id: IdModel = null;
user_id: IdModel = null;
validate = false;
2020-07-30 14:23:51 +02:00
notes: {
comment: string,
sample_references: {sample_id: IdModel, relation: string}[],
custom_fields: {[prop: string]: string}
} = {comment: '', sample_references: [], custom_fields: {}};
2020-07-27 17:52:03 +02:00
added: Date = null;
deserialize(input: any): this {
Object.assign(this, input);
if (input.hasOwnProperty('material')) {
this.material = new MaterialModel().deserialize(input.material);
this.material_id = input.material._id;
}
if (input.hasOwnProperty('measurements')) {
this.measurements = input.measurements.map(e => new MeasurementModel().deserialize(e));
}
2020-07-27 17:52:03 +02:00
if (input.hasOwnProperty('added')) {
this.added = new Date(input.added);
}
return this;
}
sendFormat() {
return pick(this.conditionTemplateCheck(), ['color', 'type', 'batch', 'condition', 'material_id', 'notes']);
}
private conditionTemplateCheck() {
const res = cloneDeep(this);
if (res.condition.condition_template === null) {
res.condition = {};
}
return res;
}
}