2023-02-21 13:23:07 -06:00
|
|
|
import { type RecordValue } from './RecordValue.js';
|
|
|
|
|
import { SaveRecordRequest } from './SaveRecordRequest.js';
|
2023-02-09 22:21:20 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @class Record - A record in an app.
|
|
|
|
|
*/
|
|
|
|
|
export class Record {
|
|
|
|
|
/**
|
|
|
|
|
* @property {number} appId - The id of the app that the record belongs to.
|
|
|
|
|
*/
|
|
|
|
|
public appId: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @property {number} recordId - The id of the record.
|
|
|
|
|
*/
|
2023-02-13 20:21:28 -06:00
|
|
|
public recordId: number | null;
|
2023-02-09 22:21:20 -06:00
|
|
|
|
|
|
|
|
/**
|
2023-02-10 00:49:50 -06:00
|
|
|
* @property {RecordValue[]} fieldData - The data for the fields in the record.
|
2023-02-09 22:21:20 -06:00
|
|
|
*/
|
2023-02-13 12:36:21 -06:00
|
|
|
public fieldData: Array<RecordValue<any>>;
|
2023-02-09 22:21:20 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constructor - Creates a new instance of Record.
|
|
|
|
|
* @param {number} appId - The id of the app that the record belongs to.
|
|
|
|
|
* @param {number} recordId - The id of the record.
|
2023-02-13 12:36:21 -06:00
|
|
|
* @param {RecordValue<any>[]} fieldData - The data for the fields in the record.
|
2023-02-09 22:21:20 -06:00
|
|
|
* @returns {Record} - A new instance of Record.
|
|
|
|
|
*/
|
2023-02-13 12:36:21 -06:00
|
|
|
constructor(
|
|
|
|
|
appId: number,
|
2023-02-13 20:21:28 -06:00
|
|
|
recordId: number | null,
|
2023-02-13 12:36:21 -06:00
|
|
|
fieldData: Array<RecordValue<any>> = []
|
|
|
|
|
) {
|
2023-02-09 22:21:20 -06:00
|
|
|
this.appId = appId;
|
|
|
|
|
this.recordId = recordId;
|
|
|
|
|
this.fieldData = fieldData;
|
|
|
|
|
}
|
2023-02-13 12:36:21 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method addValue - Adds a value to the record.
|
|
|
|
|
* @param {RecordValue<any>} fieldData - The value to add to the record.
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
|
|
|
|
public addValue(fieldData: RecordValue<any>): void {
|
|
|
|
|
this.fieldData.push(fieldData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method addValues - Adds values to the record.
|
|
|
|
|
* @param {Array<RecordValue<any>>} fieldData - The values to add to the record.
|
|
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
|
|
|
|
public addValues(fieldData: Array<RecordValue<any>>): void {
|
|
|
|
|
this.fieldData = this.fieldData.concat(fieldData);
|
|
|
|
|
}
|
2023-02-13 17:13:52 -06:00
|
|
|
|
|
|
|
|
public convertToSaveRecordRequest(): SaveRecordRequest {
|
|
|
|
|
const fields = this.fieldData.reduce((acc, cur) => {
|
|
|
|
|
acc.set(cur.fieldId, cur.value);
|
|
|
|
|
return acc;
|
|
|
|
|
}, new Map<number, any>());
|
|
|
|
|
|
|
|
|
|
return new SaveRecordRequest(this.appId, this.recordId, fields);
|
|
|
|
|
}
|
2023-02-09 22:21:20 -06:00
|
|
|
}
|