fix: refactor to have child classes that inherit from RecordValue, but represent the distinct types of value objects that can be returned by the api. fix: added and updated tests to address this refactor

This commit is contained in:
Stevan Freeborn
2023-02-13 17:05:50 -06:00
parent ec82dd066e
commit 74d247047e
32 changed files with 1809 additions and 331 deletions
+6
View File
@@ -72,4 +72,10 @@ export enum RecordValueType {
* @type {string}
*/
FileList = 'FileList',
/**
* @constant DelegateList - The record value is a list of delegates.
* @type {string}
*/
DelegateList = 'DelegateList',
}
+267 -23
View File
@@ -1,28 +1,50 @@
import { type AxiosResponse } from 'axios';
import { DelegateType } from '../enums/DelegateType';
import { FieldStatus } from '../enums/FieldStatus';
import { FieldType } from '../enums/FieldType';
import { FileStorageSite } from '../enums/FileStorageSite';
import { FormulaOutputType } from '../enums/FormulaOutputType';
import { Multiplicity } from '../enums/Multiplicity';
import { RecordValueType } from '../enums/RecordValueType';
import { TimeSpanIncrement } from '../enums/TimeSpanIncrement';
import { TimeSpanRecurrenceType } from '../enums/TimeSpanRecurrenceType';
import { App } from './App';
import { Attachment } from './Attachment';
import { AttachmentListRecordValue } from './AttachmentListRecordValue';
import { CollectionResponse } from './CollectionResponse';
import { CreatedWithIdResponse } from './CreatedWithIdResponse';
import { DateRecordValue } from './DateRecordValue';
import { DecimalRecordValue } from './DecimalRecordValue';
import { Delegate } from './Delegate';
import { DelegateListRecordValue } from './DelegateListRecordValue';
import { Field } from './Field';
import { File } from './File';
import { FileInfo } from './FileInfo';
import { FileListRecordValue } from './FileListRecordValue';
import { FormulaField } from './FormulaField';
import { GetPagedAppsResponse } from './GetPagedAppsResponse';
import { GetPagedFieldsResponse } from './GetPagedFieldsResponse';
import { GetPagedRecordsResponse } from './GetPagedRecordsResponse';
import { GetPagedReportsResponse } from './GetPagedReportsResponse';
import { GuidListRecordValue } from './GuidListRecordValue';
import { GuidRecordValue } from './GuidRecordValue';
import { IntegerListRecordValue } from './IntegerListRecordValue';
import { IntegerRecordValue } from './IntegerRecordValue';
import { ListField } from './ListField';
import { ListItemResponse } from './ListItemResponse';
import { ListValue } from './ListValue';
import { Record } from './Record';
import { RecordValue } from './RecordValue';
import { ReferenceField } from './ReferenceField';
import { Report } from './Report';
import { ReportData } from './ReportData';
import { Row } from './Row';
import { ScoringGroup } from './ScoringGroup';
import { ScoringGroupListRecordValue } from './ScoringGroupListRecordValue';
import { StringListRecordValue } from './StringListRecordValue';
import { StringRecordValue } from './StringRecordValue';
import { TimeSpanData } from './TimeSpanData';
import { TimeSpanRecordValue } from './TimeSpanRecordValue';
import { type RecordValue } from './RecordValue';
/**
* @class ApiResponse - A generic response object for API requests.
@@ -339,13 +361,10 @@ export class ApiResponse<T> {
const apiResponse = this as ApiResponse<any>;
const records = apiResponse.data.items.map((item: any) => {
const recordValues = item.fieldData.map((fieldData: any) => {
return new RecordValue(
fieldData.type,
fieldData.fieldId,
fieldData.value
);
});
const recordValues = item.fieldData.map((recordValueItem: any) =>
ApiResponse.getRecordValueByType(recordValueItem)
);
return new Record(item.appId, item.recordId, recordValues);
});
@@ -371,13 +390,10 @@ export class ApiResponse<T> {
public asRecordType(): ApiResponse<Record> {
const apiResponse = this as ApiResponse<any>;
const recordValues = apiResponse.data.fieldData.map((fieldData: any) => {
return new RecordValue(
fieldData.type,
fieldData.fieldId,
fieldData.value
);
});
const recordValues = apiResponse.data.fieldData.map(
(recordValueItem: any) =>
ApiResponse.getRecordValueByType(recordValueItem)
);
const record = new Record(
apiResponse.data.appId,
@@ -396,13 +412,10 @@ export class ApiResponse<T> {
const apiResponse = this as ApiResponse<any>;
const records = apiResponse.data.items.map((item: any) => {
const recordValues = item.fieldData.map((fieldData: any) => {
return new RecordValue(
fieldData.type,
fieldData.fieldId,
fieldData.value
);
});
const recordValues = item.fieldData.map((recordValueItem: any) =>
ApiResponse.getRecordValueByType(recordValueItem)
);
return new Record(item.appId, item.recordId, recordValues);
});
@@ -418,8 +431,113 @@ export class ApiResponse<T> {
);
}
private static getRecordValueByType(recordValueItem: any): RecordValue<any> {
const type = RecordValueType[recordValueItem.type];
switch (type) {
case RecordValueType.String: {
return new StringRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
case RecordValueType.Integer: {
return new IntegerRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
case RecordValueType.Decimal: {
return new DecimalRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
case RecordValueType.Date: {
const date = new Date(recordValueItem.value);
return new DateRecordValue(recordValueItem.fieldId, date);
}
case RecordValueType.TimeSpan: {
const timeSpan = ApiResponse.convertToTimeSpanData(
recordValueItem.value
);
return new TimeSpanRecordValue(recordValueItem.fieldId, timeSpan);
}
case RecordValueType.Guid: {
return new GuidRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
case RecordValueType.StringList: {
return new StringListRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
case RecordValueType.IntegerList: {
return new IntegerListRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
case RecordValueType.GuidList: {
return new GuidListRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
case RecordValueType.AttachmentList: {
const attachments = recordValueItem.value.map((attachmentItem: any) =>
ApiResponse.convertToAttachment(attachmentItem)
);
return new AttachmentListRecordValue(
recordValueItem.fieldId,
attachments
);
}
case RecordValueType.ScoringGroupList: {
const array = recordValueItem.value as any[];
const isDelegateList = array[0].delegateType !== undefined;
if (isDelegateList) {
const delegates = recordValueItem.value.map((delegateItem: any) =>
ApiResponse.convertToDelegate(delegateItem)
);
return new DelegateListRecordValue(
recordValueItem.fieldId,
delegates
);
}
const scoringGroups = recordValueItem.value.map(
(scoringGroupItem: any) =>
ApiResponse.convertToScoringGroup(scoringGroupItem)
);
return new ScoringGroupListRecordValue(
recordValueItem.fieldId,
scoringGroups
);
}
case RecordValueType.FileList: {
return new FileListRecordValue(
recordValueItem.fieldId,
recordValueItem.value
);
}
default: {
throw new Error(
`Unknown record value type: ${recordValueItem.type as string}`
);
}
}
}
/**
* @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type.
* @method getFieldByType - Converts the field item to the appropriate field object based upon the field item's type.
* @param {any} fieldItem - The field item to convert.
* @returns {Field} - The converted field object.
* @throws {Error} - If the field item's type is unknown.
@@ -495,6 +613,132 @@ export class ApiResponse<T> {
}
}
private static convertToDelegate(delegateItem: any): Delegate {
const delegateType = DelegateType[delegateItem.delegateType];
if (delegateType === undefined) {
throw new Error(
`${delegateItem.delegateType as string} is not a valid DelegateType.`
);
}
const delegationDateTime = new Date(delegateItem.delegationDateTime);
const hasName =
delegateItem.name !== null && delegateItem.name !== undefined;
const name = hasName ? delegateItem.name : null;
const hasCompletionDate =
delegateItem.delegationCompletedDateTime !== null &&
delegateItem.delegationCompletedDateTime !== undefined;
const delegationCompletedDateTime = hasCompletionDate
? new Date(delegateItem.delegationCompletedDateTime)
: null;
return new Delegate(
delegateItem.delegateType,
name,
delegateItem.emailAddress,
delegationDateTime,
delegationCompletedDateTime
);
}
/**
* @method convertToScoringGroup - Converts the scoring group item to the appropriate scoring group object.
* @param {any} scoringGroupItem - The scoring group item to convert.
* @returns {ScoringGroup} - The converted scoring group object.
*/
private static convertToScoringGroup(scoringGroupItem: any): ScoringGroup {
return new ScoringGroup(
scoringGroupItem.id,
scoringGroupItem.name,
scoringGroupItem.score,
scoringGroupItem.maximumScore
);
}
/**
* @method convertToAttachment - Converts the attachment item to the appropriate attachment object.
* @param {any} attachmentItem - The attachment item to convert.
* @returns {Attachment} - The converted attachment object.
*/
private static convertToAttachment(attachmentItem: any): Attachment {
const storageLocation = FileStorageSite[attachmentItem.storageLocation];
if (storageLocation === undefined) {
throw new Error(
`${
attachmentItem.storageLocation as string
} is not a valid FileStorageSite.`
);
}
const hasNotes =
attachmentItem.notes !== null && attachmentItem.notes !== undefined;
const notes = hasNotes ? attachmentItem.notes : null;
return new Attachment(
attachmentItem.fileId,
attachmentItem.fileName,
notes,
storageLocation
);
}
/**
* @method convertToTimeSpanData - Converts the time span item to the appropriate time span data object.
* @param {any} timeSpanItem - The time span item to convert.
* @returns {TimeSpanData} - The converted time span data object.
*/
private static convertToTimeSpanData(timeSpanItem: any): TimeSpanData {
const increment = TimeSpanIncrement[timeSpanItem.increment];
const hasRecurrence =
timeSpanItem.recurrence !== null && timeSpanItem.recurrence !== undefined;
const recurrene = hasRecurrence
? TimeSpanRecurrenceType[timeSpanItem.recurrence]
: null;
const hasEndAfterOccurrences =
timeSpanItem.endAfterOccurrences !== null &&
timeSpanItem.endAfterOccurrences !== undefined;
const endAfterOccurrences = hasEndAfterOccurrences
? timeSpanItem.endAfterOccurrences
: null;
const hasEndByDate =
timeSpanItem.endByDate !== null && timeSpanItem.endByDate !== undefined;
const endByDate = hasEndByDate ? new Date(timeSpanItem.endByDate) : null;
if (increment === undefined) {
throw new Error(
`${timeSpanItem.increment as string} is not a valid TimeSpanIncrement.`
);
}
if (recurrene === undefined) {
throw new Error(
`${
timeSpanItem.recurrence as string
} is not a valid TimeSpanRecurrenceType.`
);
}
return new TimeSpanData(
timeSpanItem.quantity,
increment,
recurrene,
endAfterOccurrences,
endByDate
);
}
/**
* @method getListValues - Converts the field items list values to a ListValue[].
* @param fieldItem - The field item whose values should be converted.
+18
View File
@@ -0,0 +1,18 @@
import { RecordValueType } from '../enums/RecordValueType';
import { type Attachment } from './Attachment';
import { RecordValue } from './RecordValue';
/**
* @class AttachmentListRecordValue - A record value represented by a list of attachments.
*/
export class AttachmentListRecordValue extends RecordValue<Attachment[]> {
/**
* @constructor - Creates a new instance of AttachmentListRecordValue.
* @param {number} fieldId - The id of the field.
* @param {Attachment[]} value - The value of the field.
* @returns {AttachmentListRecordValue} - A new instance of AttachmentListRecordValue.
*/
constructor(fieldId: number, value: Attachment[]) {
super(RecordValueType.AttachmentList, fieldId, value);
}
}
+17
View File
@@ -0,0 +1,17 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
/**
* @class DateRecordValue - A value for a date field in a record.
*/
export class DateRecordValue extends RecordValue<Date> {
/**
* @constructor - Creates a new instance of DateRecordValue.
* @param {number} fieldId - The id of the field.
* @param {Date} value - The value of the field.
* @returns {DateRecordValue} - A new instance of DateRecordValue.
*/
constructor(fieldId: number, value: Date) {
super(RecordValueType.Date, fieldId, value);
}
}
+8
View File
@@ -0,0 +1,8 @@
import { RecordValue } from './RecordValue';
import { RecordValueType } from '../enums/RecordValueType';
export class DecimalRecordValue extends RecordValue<number> {
constructor(fieldId: number, value: number) {
super(RecordValueType.Decimal, fieldId, value);
}
}
+9
View File
@@ -0,0 +1,9 @@
import { RecordValue } from './RecordValue';
import { RecordValueType } from '../enums/RecordValueType';
import { type Delegate } from './Delegate';
export class DelegateListRecordValue extends RecordValue<Delegate[]> {
constructor(fieldId: number, value: Delegate[]) {
super(RecordValueType.DelegateList, fieldId, value);
}
}
+9
View File
@@ -0,0 +1,9 @@
import { RecordValue } from './RecordValue';
import { RecordValueType } from '../enums/RecordValueType';
import { type File } from './File';
export class FileListRecordValue extends RecordValue<File[]> {
constructor(fieldId: number, value: File[]) {
super(RecordValueType.FileList, fieldId, value);
}
}
+8
View File
@@ -0,0 +1,8 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
export class GuidListRecordValue extends RecordValue<string[]> {
constructor(fieldId: number, value: string[]) {
super(RecordValueType.GuidList, fieldId, value);
}
}
+8
View File
@@ -0,0 +1,8 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
export class GuidRecordValue extends RecordValue<string> {
constructor(fieldId: number, value: string) {
super(RecordValueType.Guid, fieldId, value);
}
}
+8
View File
@@ -0,0 +1,8 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
export class IntegerListRecordValue extends RecordValue<number[]> {
constructor(fieldId: number, value: number[]) {
super(RecordValueType.IntegerList, fieldId, value);
}
}
+17
View File
@@ -0,0 +1,17 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
/**
* @class IntegerRecordValue - A record value represented by an integer.
*/
export class IntegerRecordValue extends RecordValue<number> {
/**
* @constructor - Creates a new instance of IntegerRecordValue.
* @param {number} fieldId - The id of the field.
* @param {number} value - The value of the field.
* @returns {IntgerRecordValue} - A new instance of IntegerRecordValue.
*/
constructor(fieldId: number, value: number) {
super(RecordValueType.Integer, fieldId, value);
}
}
+21 -126
View File
@@ -1,12 +1,12 @@
import { DelegateType } from '../enums/DelegateType';
import { FileStorageSite } from '../enums/FileStorageSite';
import { RecordValueType } from '../enums/RecordValueType';
import { TimeSpanIncrement } from '../enums/TimeSpanIncrement';
import { TimeSpanRecurrenceType } from '../enums/TimeSpanRecurrenceType';
import { Attachment } from './Attachment';
import { Delegate } from './Delegate';
import { ScoringGroup } from './ScoringGroup';
import { TimeSpanData } from './TimeSpanData';
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';
/**
* @class RecordValue - A value for a field in a record.
@@ -23,9 +23,9 @@ export class RecordValue<T> {
public fieldId: number;
/**
* @property {T | any} value - The value of the field.
* @property {T} value - The value of the field.
*/
public value: T | any;
public value: T;
/**
* @constructor - Creates a new instance of RecordValue.
@@ -34,7 +34,7 @@ export class RecordValue<T> {
* @param {any} value - The value of the field.
* @returns {RecordValue} - A new instance of RecordValue.
*/
constructor(type: RecordValueType, fieldId: number, value: T | any) {
constructor(type: RecordValueType, fieldId: number, value: T) {
this.type = type;
this.fieldId = fieldId;
this.value = value;
@@ -47,7 +47,7 @@ export class RecordValue<T> {
*/
public asString(): string {
this.validateType([RecordValueType.String, RecordValueType.Guid]);
return this.value;
return this.value as string;
}
/**
@@ -57,7 +57,7 @@ export class RecordValue<T> {
*/
public asNumber(): number {
this.validateType([RecordValueType.Integer, RecordValueType.Decimal]);
return this.value;
return this.value as number;
}
/**
@@ -67,7 +67,7 @@ export class RecordValue<T> {
*/
public asDate(): Date {
this.validateType([RecordValueType.Date]);
return new Date(this.value);
return this.value as Date;
}
/**
@@ -78,29 +78,7 @@ export class RecordValue<T> {
*/
public asAttachmentArray(): Attachment[] {
this.validateType([RecordValueType.AttachmentList]);
return this.value.map((attachment: any) => {
const storageLocation = FileStorageSite[attachment.storageLocation];
if (storageLocation === undefined) {
throw new Error(
`${
this.value.storageLocation as string
} is not a valid FileStorageSite.`
);
}
const hasNotes =
attachment.notes !== null && attachment.notes !== undefined;
const notes = hasNotes ? attachment.notes : null;
return new Attachment(
attachment.fileId,
attachment.fileName,
notes,
storageLocation
);
});
return (this as AttachmentListRecordValue).value;
}
/**
@@ -110,7 +88,7 @@ export class RecordValue<T> {
*/
public asNumberArray(): number[] {
this.validateType([RecordValueType.FileList, RecordValueType.IntegerList]);
return this.value;
return this.value as number[];
}
/**
@@ -120,47 +98,17 @@ export class RecordValue<T> {
*/
public asStringArray(): string[] {
this.validateType([RecordValueType.StringList, RecordValueType.GuidList]);
return this.value;
return this.value as string[];
}
/**
* @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.
* @throws {Error} - If a delegate type is not valid.
*/
public asDelegateArray(): Delegate[] {
this.validateType([RecordValueType.ScoringGroupList]);
return this.value.map((delegate: any) => {
const delegateType = DelegateType[delegate.delegateType];
if (delegateType === undefined) {
throw new Error(
`${delegate.delegateType as string} is not a valid DelegateType.`
);
}
const delegationDateTime = new Date(delegate.delegationDateTime);
const hasName = delegate.name !== null && delegate.name !== undefined;
const name = hasName ? delegate.name : null;
const hasCompletionDate =
delegate.delegationCompletedDateTime !== null &&
delegate.delegationCompletedDateTime !== undefined;
const delegationCompletedDateTime = hasCompletionDate
? new Date(delegate.delegationCompletedDateTime)
: null;
return new Delegate(
delegate.delegateType,
name,
delegate.emailAddress,
delegationDateTime,
delegationCompletedDateTime
);
});
this.validateType([RecordValueType.DelegateList]);
return (this as DelegateListRecordValue).value;
}
/**
@@ -170,70 +118,17 @@ export class RecordValue<T> {
*/
public asScoringGroupArray(): ScoringGroup[] {
this.validateType([RecordValueType.ScoringGroupList]);
return this.value.map(
(scoringGroup: any) =>
new ScoringGroup(
scoringGroup.listValueId,
scoringGroup.name,
scoringGroup.score,
scoringGroup.maximumScore
)
);
return (this as ScoringGroupListRecordValue).value;
}
/**
* @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.
* @throws {Error} - If the increment is not valid.
* @throws {Error} - If the recurrence is not valid.
*/
public asTimeSpanData(): TimeSpanData {
this.validateType([RecordValueType.TimeSpan]);
const increment = TimeSpanIncrement[this.value.increment];
const hasRecurrence =
this.value.recurrence !== null && this.value.recurrence !== undefined;
const recurrene = hasRecurrence
? TimeSpanRecurrenceType[this.value.recurrence]
: null;
const hasEndAfterOccurrences =
this.value.endAfterOccurrences !== null &&
this.value.endAfterOccurrences !== undefined;
const endAfterOccurrences = hasEndAfterOccurrences
? this.value.endAfterOccurrences
: null;
const hasEndByDate =
this.value.endByDate !== null && this.value.endByDate !== undefined;
const endByDate = hasEndByDate ? new Date(this.value.endByDate) : null;
if (increment === undefined) {
throw new Error(
`${this.value.increment as string} is not a valid TimeSpanIncrement.`
);
}
if (recurrene === undefined) {
throw new Error(
`${
this.value.recurrence as string
} is not a valid TimeSpanRecurrenceType.`
);
}
return new TimeSpanData(
this.value.quantity,
increment,
recurrene,
endAfterOccurrences,
endByDate
);
return (this as TimeSpanRecordValue).value;
}
/**
@@ -0,0 +1,9 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
import { type ScoringGroup } from './ScoringGroup';
export class ScoringGroupListRecordValue extends RecordValue<ScoringGroup[]> {
constructor(fieldId: number, value: ScoringGroup[]) {
super(RecordValueType.ScoringGroupList, fieldId, value);
}
}
+8
View File
@@ -0,0 +1,8 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
export class StringListRecordValue extends RecordValue<string[]> {
constructor(fieldId: number, value: string[]) {
super(RecordValueType.StringList, fieldId, value);
}
}
+17
View File
@@ -0,0 +1,17 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
/**
* @class StringRecordValue - A record value represented by a string.
*/
export class StringRecordValue extends RecordValue<string> {
/**
* @constructor - Creates a new instance of StringRecordValue.
* @param {number} fieldId - The id of the field.
* @param {string} value - The value of the field.
* @returns {StringRecordValue} - A new instance of StringRecordValue.
*/
constructor(fieldId: number, value: string) {
super(RecordValueType.String, fieldId, value);
}
}
+9
View File
@@ -0,0 +1,9 @@
import { RecordValueType } from '../enums/RecordValueType';
import { RecordValue } from './RecordValue';
import { type TimeSpanData } from './TimeSpanData';
export class TimeSpanRecordValue extends RecordValue<TimeSpanData> {
constructor(fieldId: number, value: TimeSpanData) {
super(RecordValueType.TimeSpan, fieldId, value);
}
}