2023-02-10 00:49:50 -06:00
|
|
|
import { RecordValueType } from '../enums/RecordValueType';
|
2023-02-13 17:05:50 -06:00
|
|
|
import { type Attachment } from './Attachment';
|
|
|
|
|
import { type AttachmentListRecordValue } from './AttachmentListRecordValue';
|
|
|
|
|
import { type Delegate } from './Delegate';
|
|
|
|
|
import { type DelegateListRecordValue } from './DelegateListRecordValue';
|
|
|
|
|
import { type ScoringGroup } from './ScoringGroup';
|
|
|
|
|
import { type ScoringGroupListRecordValue } from './ScoringGroupListRecordValue';
|
|
|
|
|
import { type TimeSpanData } from './TimeSpanData';
|
|
|
|
|
import { type TimeSpanRecordValue } from './TimeSpanRecordValue';
|
2023-02-09 22:21:20 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @class RecordValue - A value for a field in a record.
|
|
|
|
|
*/
|
2023-02-13 12:36:21 -06:00
|
|
|
export class RecordValue<T> {
|
2023-02-09 22:21:20 -06:00
|
|
|
/**
|
|
|
|
|
* @property {RecordValueType} type - The type of the record value.
|
|
|
|
|
*/
|
|
|
|
|
public type: RecordValueType;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @property {number} fieldId - The id of the field.
|
|
|
|
|
*/
|
|
|
|
|
public fieldId: number;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-02-13 17:05:50 -06:00
|
|
|
* @property {T} value - The value of the field.
|
2023-02-09 22:21:20 -06:00
|
|
|
*/
|
2023-02-13 17:05:50 -06:00
|
|
|
public value: T;
|
2023-02-09 22:21:20 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constructor - Creates a new instance of RecordValue.
|
|
|
|
|
* @param {RecordValueType} type - The type of the record value.
|
|
|
|
|
* @param {number} fieldId - The id of the field.
|
2023-02-10 00:49:50 -06:00
|
|
|
* @param {any} value - The value of the field.
|
2023-02-09 22:21:20 -06:00
|
|
|
* @returns {RecordValue} - A new instance of RecordValue.
|
|
|
|
|
*/
|
2023-02-13 17:05:50 -06:00
|
|
|
constructor(type: RecordValueType, fieldId: number, value: T) {
|
2023-02-09 22:21:20 -06:00
|
|
|
this.type = type;
|
|
|
|
|
this.fieldId = fieldId;
|
|
|
|
|
this.value = value;
|
|
|
|
|
}
|
2023-02-10 00:49:50 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asString - Gets the value as a string.
|
|
|
|
|
* @returns {string} - The value as a string.
|
|
|
|
|
* @throws {Error} - If the value is not a string.
|
|
|
|
|
*/
|
|
|
|
|
public asString(): string {
|
|
|
|
|
this.validateType([RecordValueType.String, RecordValueType.Guid]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return this.value as string;
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asNumber - Gets the value as a number.
|
|
|
|
|
* @returns {number} - The value as a number.
|
|
|
|
|
* @throws {Error} - If the value is not a number.
|
|
|
|
|
*/
|
|
|
|
|
public asNumber(): number {
|
|
|
|
|
this.validateType([RecordValueType.Integer, RecordValueType.Decimal]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return this.value as number;
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asDate - Gets the value as a date.
|
|
|
|
|
* @returns {Date} - The value as a date.
|
|
|
|
|
* @throws {Error} - If the value is not a date.
|
|
|
|
|
*/
|
|
|
|
|
public asDate(): Date {
|
|
|
|
|
this.validateType([RecordValueType.Date]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return this.value as Date;
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asAttachmentArray - Gets the value as an array of attachments.
|
|
|
|
|
* @returns {Attachment[]} - The value as an array of attachments.
|
|
|
|
|
* @throws {Error} - If the value is not an array of attachments.
|
|
|
|
|
* @throws {Error} - If the storage location is not a valid FileStorageSite.
|
|
|
|
|
*/
|
|
|
|
|
public asAttachmentArray(): Attachment[] {
|
|
|
|
|
this.validateType([RecordValueType.AttachmentList]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return (this as AttachmentListRecordValue).value;
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asNumberArray - Gets the value as an array of numbers.
|
|
|
|
|
* @returns {number[]} - The value as an array of numbers.
|
|
|
|
|
* @throws {Error} - If the value is not an array of numbers.
|
|
|
|
|
*/
|
|
|
|
|
public asNumberArray(): number[] {
|
|
|
|
|
this.validateType([RecordValueType.FileList, RecordValueType.IntegerList]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return this.value as number[];
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asStringArray - Gets the value as an array of strings.
|
|
|
|
|
* @returns {string[]} - The value as an array of strings.
|
|
|
|
|
* @throws {Error} - If the value is not an array of strings.
|
|
|
|
|
*/
|
|
|
|
|
public asStringArray(): string[] {
|
|
|
|
|
this.validateType([RecordValueType.StringList, RecordValueType.GuidList]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return this.value as string[];
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asDelegateArray - Gets the value as an array of delegates.
|
|
|
|
|
* @returns {Delegate[]} - The value as an array of delegates.
|
|
|
|
|
* @throws {Error} - If the value is not an array of delegates.
|
|
|
|
|
*/
|
|
|
|
|
public asDelegateArray(): Delegate[] {
|
2023-02-13 17:05:50 -06:00
|
|
|
this.validateType([RecordValueType.DelegateList]);
|
|
|
|
|
return (this as DelegateListRecordValue).value;
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asScoringGroupArray - Gets the value as an array of scoring groups.
|
|
|
|
|
* @returns {ScoringGroup[]} - The value as an array of scoring groups.
|
|
|
|
|
* @throws {Error} - If the value is not an array of scoring groups.
|
|
|
|
|
*/
|
|
|
|
|
public asScoringGroupArray(): ScoringGroup[] {
|
|
|
|
|
this.validateType([RecordValueType.ScoringGroupList]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return (this as ScoringGroupListRecordValue).value;
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method asTimeSpanData - Gets the value as a TimeSpanData object.
|
|
|
|
|
* @returns {TimeSpanData} - The value as a TimeSpanData object.
|
|
|
|
|
* @throws {Error} - If the value is not a TimeSpanData object.
|
|
|
|
|
*/
|
|
|
|
|
public asTimeSpanData(): TimeSpanData {
|
|
|
|
|
this.validateType([RecordValueType.TimeSpan]);
|
2023-02-13 17:05:50 -06:00
|
|
|
return (this as TimeSpanRecordValue).value;
|
2023-02-10 00:49:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method validateType - Validates the type of the field value.
|
|
|
|
|
* @param {RecordValueType[]} expectedTypes - The expected types.
|
|
|
|
|
* @throws {Error} - If the type is not valid.
|
|
|
|
|
*/
|
|
|
|
|
private validateType(expectedTypes: RecordValueType[]): void {
|
|
|
|
|
if (expectedTypes.includes(this.type) === false) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Unable to get value for field value. Field value type must be of the following types: ${expectedTypes.join(
|
|
|
|
|
', '
|
|
|
|
|
)}. Actual type: ${this.type}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 22:21:20 -06:00
|
|
|
}
|