fix: implement formula field, list field, and reference field as chilrden of the field class. fix: when converting api response data to appropriate field response make sure json field objects are converted to correct field class based upon type.
This commit is contained in:
@@ -68,6 +68,11 @@ export enum FieldType {
|
||||
*/
|
||||
SurveyUnifiedAnswer = 'SurveyUnifiedAnswer',
|
||||
|
||||
/**
|
||||
* @constant SurveyDelegation - The field is a survey delegation field.
|
||||
*/
|
||||
SurveyDelegation = 'SurveyDelegation',
|
||||
|
||||
/**
|
||||
* @constant Attachment - The field is a attachment field.
|
||||
* @type {string}
|
||||
|
||||
+95
-33
@@ -1,10 +1,15 @@
|
||||
import { FieldType } from '../enums/FieldType';
|
||||
import { App } from './App';
|
||||
import { CollectionResponse } from './CollectionResponse';
|
||||
import { CreatedWithIdResponse } from './CreatedWithIdResponse';
|
||||
import { Field } from './Field';
|
||||
import { FileInfo } from './FileInfo';
|
||||
import { FormulaField } from './FormulaField';
|
||||
import { GetPagedAppsResponse } from './GetPagedAppsResponse';
|
||||
import { GetPagedFieldsResponse } from './GetPagedFieldsResponse';
|
||||
import { ListField } from './ListField';
|
||||
import { ListValue } from './ListValue';
|
||||
import { ReferenceField } from './ReferenceField';
|
||||
|
||||
/**
|
||||
* @class ApiResponse - A generic response object for API requests.
|
||||
@@ -119,16 +124,7 @@ export class ApiResponse<T> {
|
||||
*/
|
||||
public asFieldType(): ApiResponse<Field> {
|
||||
const apiResponse = this as ApiResponse<any>;
|
||||
|
||||
const field = new Field(
|
||||
apiResponse.data.id,
|
||||
apiResponse.data.appId,
|
||||
apiResponse.data.name,
|
||||
apiResponse.data.type,
|
||||
apiResponse.data.status,
|
||||
apiResponse.data.isRequired,
|
||||
apiResponse.data.isUnique
|
||||
);
|
||||
const field = ApiResponse.getFieldByType(apiResponse.data);
|
||||
|
||||
return new ApiResponse<Field>(
|
||||
apiResponse.statusCode,
|
||||
@@ -144,17 +140,9 @@ export class ApiResponse<T> {
|
||||
public asFieldCollectionType(): ApiResponse<CollectionResponse<Field>> {
|
||||
const apiResponse = this as ApiResponse<any>;
|
||||
|
||||
const fields = apiResponse.data.items.map((item: any) => {
|
||||
return new Field(
|
||||
item.id,
|
||||
item.appId,
|
||||
item.name,
|
||||
item.type,
|
||||
item.status,
|
||||
item.isRequired,
|
||||
item.isUnique
|
||||
);
|
||||
});
|
||||
const fields = apiResponse.data.items.map((item: any) =>
|
||||
ApiResponse.getFieldByType(item)
|
||||
);
|
||||
|
||||
const collectionResponse = new CollectionResponse<Field>(
|
||||
apiResponse.data.count,
|
||||
@@ -175,17 +163,9 @@ export class ApiResponse<T> {
|
||||
public asGetPagedFieldsResponseType(): ApiResponse<GetPagedFieldsResponse> {
|
||||
const apiResponse = this as ApiResponse<any>;
|
||||
|
||||
const fields = apiResponse.data.items.map((item: any) => {
|
||||
return new Field(
|
||||
item.id,
|
||||
item.appId,
|
||||
item.name,
|
||||
item.type,
|
||||
item.status,
|
||||
item.isRequired,
|
||||
item.isUnique
|
||||
);
|
||||
});
|
||||
const fields = apiResponse.data.items.map((item: any) =>
|
||||
ApiResponse.getFieldByType(item)
|
||||
);
|
||||
|
||||
const getPagedFieldsResponse = new GetPagedFieldsResponse(
|
||||
fields,
|
||||
@@ -220,7 +200,7 @@ export class ApiResponse<T> {
|
||||
);
|
||||
}
|
||||
|
||||
asFileInfoType(): ApiResponse<FileInfo> {
|
||||
public asFileInfoType(): ApiResponse<FileInfo> {
|
||||
const apiResponse = this as ApiResponse<any>;
|
||||
|
||||
const fileInfo = new FileInfo(
|
||||
@@ -240,4 +220,86 @@ export class ApiResponse<T> {
|
||||
fileInfo
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type.
|
||||
* @param fieldItem - The field item to convert.
|
||||
* @returns {Field} - The converted field object.
|
||||
*/
|
||||
private static getFieldByType(fieldItem: any): Field {
|
||||
switch (fieldItem.type) {
|
||||
case FieldType.Reference: {
|
||||
return new ReferenceField(
|
||||
fieldItem.id,
|
||||
fieldItem.appId,
|
||||
fieldItem.name,
|
||||
fieldItem.type,
|
||||
fieldItem.status,
|
||||
fieldItem.isRequired,
|
||||
fieldItem.isUnique,
|
||||
fieldItem.multiplicity,
|
||||
fieldItem.referencedAppId
|
||||
);
|
||||
}
|
||||
case FieldType.List: {
|
||||
const values = ApiResponse.getListValues(fieldItem);
|
||||
|
||||
return new ListField(
|
||||
fieldItem.id,
|
||||
fieldItem.appId,
|
||||
fieldItem.name,
|
||||
fieldItem.type,
|
||||
fieldItem.status,
|
||||
fieldItem.isRequired,
|
||||
fieldItem.isUnique,
|
||||
fieldItem.multiplicity,
|
||||
fieldItem.listId,
|
||||
values
|
||||
);
|
||||
}
|
||||
case FieldType.Formula: {
|
||||
const values = ApiResponse.getListValues(fieldItem);
|
||||
|
||||
return new FormulaField(
|
||||
fieldItem.id,
|
||||
fieldItem.appId,
|
||||
fieldItem.name,
|
||||
fieldItem.type,
|
||||
fieldItem.status,
|
||||
fieldItem.isRequired,
|
||||
fieldItem.isUnique,
|
||||
fieldItem.outputType,
|
||||
values
|
||||
);
|
||||
}
|
||||
default: {
|
||||
return new Field(
|
||||
fieldItem.id,
|
||||
fieldItem.appId,
|
||||
fieldItem.name,
|
||||
fieldItem.type,
|
||||
fieldItem.status,
|
||||
fieldItem.isRequired,
|
||||
fieldItem.isUnique
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @method getListValues - Converts the field items list values to a ListValue[].
|
||||
* @param fieldItem - The field item whose values should be converted.
|
||||
* @returns {ListValue[]} - The converted list values.
|
||||
*/
|
||||
private static getListValues(fieldItem: any): ListValue[] {
|
||||
return fieldItem.values.map((listValue: any) => {
|
||||
return new ListValue(
|
||||
listValue.id,
|
||||
listValue.name,
|
||||
listValue.sortOrder,
|
||||
listValue.numericValue,
|
||||
listValue.color
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { FormulaOutputType } from '../enums/FormulaOutputType';
|
||||
import { Field } from './Field';
|
||||
import { type ListValue } from './ListValue';
|
||||
|
||||
export class FormulaField extends Field {
|
||||
public outputType: FormulaOutputType;
|
||||
public values: ListValue[];
|
||||
|
||||
constructor(
|
||||
id: number,
|
||||
appId: number,
|
||||
name: string,
|
||||
type: string,
|
||||
status: string,
|
||||
isRequired: boolean,
|
||||
isUnique: boolean,
|
||||
outputType: string,
|
||||
values: ListValue[]
|
||||
) {
|
||||
super(id, appId, name, type, status, isRequired, isUnique);
|
||||
|
||||
if (FormulaOutputType[outputType] === undefined) {
|
||||
throw new Error(
|
||||
`The outputType ${outputType} is not a valid FormulaOutputType.`
|
||||
);
|
||||
}
|
||||
|
||||
this.outputType = FormulaOutputType[outputType];
|
||||
this.values = values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { Multiplicity } from '../enums/Multiplicity';
|
||||
import { Field } from './Field';
|
||||
import { type ListValue } from './ListValue';
|
||||
|
||||
export class ListField extends Field {
|
||||
public multiplicity: Multiplicity;
|
||||
public listId: number;
|
||||
public values: ListValue[];
|
||||
|
||||
constructor(
|
||||
id: number,
|
||||
appId: number,
|
||||
name: string,
|
||||
type: string,
|
||||
status: string,
|
||||
isRequired: boolean,
|
||||
isUnique: boolean,
|
||||
multiplicity: string,
|
||||
listId: number,
|
||||
values: ListValue[]
|
||||
) {
|
||||
super(id, appId, name, type, status, isRequired, isUnique);
|
||||
|
||||
if (Multiplicity[multiplicity] === undefined) {
|
||||
throw new Error(
|
||||
`The multiplicity ${multiplicity} is not a valid Multiplicity.`
|
||||
);
|
||||
}
|
||||
|
||||
this.multiplicity = Multiplicity[multiplicity];
|
||||
this.listId = listId;
|
||||
this.values = values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
export class ListValue {
|
||||
public id: string;
|
||||
public name: string;
|
||||
public sortOrder: number;
|
||||
public numericValue: number;
|
||||
public color: string;
|
||||
|
||||
constructor(
|
||||
id: string,
|
||||
name: string,
|
||||
sortOrder: number,
|
||||
numericValue: number,
|
||||
color: string
|
||||
) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.sortOrder = sortOrder;
|
||||
this.numericValue = numericValue;
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Multiplicity } from '../enums/Multiplicity';
|
||||
import { Field } from './Field';
|
||||
|
||||
export class ReferenceField extends Field {
|
||||
public multiplicity: Multiplicity;
|
||||
public referencedAppId: number;
|
||||
|
||||
constructor(
|
||||
id: number,
|
||||
appId: number,
|
||||
name: string,
|
||||
type: string,
|
||||
status: string,
|
||||
isRequired: boolean,
|
||||
isUnique: boolean,
|
||||
multiplicity: string,
|
||||
referencedAppId: number
|
||||
) {
|
||||
super(id, appId, name, type, status, isRequired, isUnique);
|
||||
|
||||
if (Multiplicity[multiplicity] === undefined) {
|
||||
throw new Error(
|
||||
`The multiplicity ${multiplicity} is not a valid Multiplicity.`
|
||||
);
|
||||
}
|
||||
|
||||
this.multiplicity = Multiplicity[multiplicity];
|
||||
this.referencedAppId = referencedAppId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user