Skip to content

Commit

Permalink
Merge pull request #2464 from ideafast/feat/trpc-study
Browse files Browse the repository at this point in the history
Feat/trpc study
  • Loading branch information
wsy19961129 authored Jul 12, 2024
2 parents 8444280 + 3294027 commit 1e5e0c7
Show file tree
Hide file tree
Showing 10 changed files with 1,424 additions and 43 deletions.
69 changes: 33 additions & 36 deletions packages/itmat-cores/src/GraphQLCore/studyCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ import { buildPipeline, dataStandardization } from '../utils/query';
import { DBType } from '../database/database';
import { ObjectStore } from '@itmat-broker/itmat-commons';

enum enumV2DataType {
'INT' = 'int',
'DEC' = 'dec',
'STR' = 'str',
'BOOL' = 'bool',
'DATE' = 'date',
'FILE' = 'file',
'JSON' = 'json',
'CAT' = 'cat'
}

export interface CreateFieldInput {
fieldId: string;
fieldName: string
tableName?: string
dataType: string;
dataType: enumV2DataType;
possibleValues?: ICategoricalOption[]
unit?: string
comments?: string
Expand All @@ -28,7 +39,7 @@ export interface EditFieldInput {
fieldId: string;
fieldName: string;
tableName?: string;
dataType: string;
dataType: enumV2DataType;
possibleValues?: ICategoricalOption[]
unit?: string
comments?: string
Expand Down Expand Up @@ -107,28 +118,35 @@ export class StudyCore {
return {
id: el.id,
code: el.code,
description: el.description
description: el.description,
life: {
createdTime: el.life.createdTime,
createdUser: el.life.createdUser,
deletedTime: el.life.deletedTime,
deletedUser: el.life.deletedUser
},
metadata: {}
};
}) : [],
dataType: (() => {
if (field.dataType === enumDataTypes.INTEGER) {
return 'int';
return enumV2DataType.INT;
} else if (field.dataType === enumDataTypes.DECIMAL) {
return 'dec';
return enumV2DataType.DEC;
} else if (field.dataType === enumDataTypes.STRING) {
return 'str';
return enumV2DataType.STR;
} else if (field.dataType === enumDataTypes.BOOLEAN) {
return 'bool';
return enumV2DataType.BOOL;
} else if (field.dataType === enumDataTypes.DATETIME) {
return 'date';
return enumV2DataType.DATE;
} else if (field.dataType === enumDataTypes.FILE) {
return 'file';
return enumV2DataType.FILE;
} else if (field.dataType === enumDataTypes.JSON) {
return 'json';
return enumV2DataType.JSON;
} else if (field.dataType === enumDataTypes.CATEGORICAL) {
return 'cat';
return enumV2DataType.CAT;
} else {
return 'str';
return enumV2DataType.STR;
}
})(),
dateAdded: field.life.createdTime.toString(),
Expand Down Expand Up @@ -654,7 +672,7 @@ export class StudyCore {
error.push(`Data type shouldn't be ${fieldEntry.dataType}: use 'int' for integer, 'dec' for decimal, 'str' for string, 'bool' for boolean, 'date' for datetime, 'file' for FILE, 'json' for json.`);
}
// check possiblevalues to be not-empty if datatype is categorical
if (fieldEntry.dataType === enumDataTypes.CATEGORICAL) {
if (fieldEntry.dataType === enumV2DataType.CAT) {
if (fieldEntry.possibleValues !== undefined && fieldEntry.possibleValues !== null) {
if (fieldEntry.possibleValues.length === 0) {
error.push(`${fieldEntry.fieldId}-${fieldEntry.fieldName}: possible values can't be empty if data type is categorical.`);
Expand All @@ -672,7 +690,7 @@ export class StudyCore {
fieldName: fieldEntry.fieldName,
tableName: null,
dataType: fieldEntry.dataType,
possibleValues: fieldEntry.dataType === enumDataTypes.CATEGORICAL ? fieldEntry.possibleValues : null,
possibleValues: fieldEntry.dataType === enumV2DataType.CAT ? fieldEntry.possibleValues : null,
unit: fieldEntry.unit,
comments: fieldEntry.comments,
metadata: {
Expand Down Expand Up @@ -808,27 +826,6 @@ export class StudyCore {
}
searchField.fieldId = fieldInput.fieldId;
searchField.fieldName = fieldInput.fieldName;
searchField.dataType = (() => {
if (fieldInput.dataType === 'int') {
return enumDataTypes.INTEGER;
} else if (fieldInput.dataType === 'dec') {
return enumDataTypes.DECIMAL;
} else if (fieldInput.dataType === 'str') {
return enumDataTypes.STRING;
} else if (fieldInput.dataType === 'bool') {
return enumDataTypes.BOOLEAN;
} else if (fieldInput.dataType === 'date') {
return enumDataTypes.DATETIME;
} else if (fieldInput.dataType === 'file') {
return enumDataTypes.FILE;
} else if (fieldInput.dataType === 'json') {
return enumDataTypes.JSON;
} else if (fieldInput.dataType === 'cat') {
return enumDataTypes.CATEGORICAL;
} else {
return enumDataTypes.STRING;
}
})();
if (fieldInput.tableName) {
searchField.metadata['tableName'] = fieldInput.tableName;
}
Expand All @@ -845,7 +842,7 @@ export class StudyCore {
searchField.comments = fieldInput.comments;
}

const { error } = await this.validateAndGenerateFieldEntry(searchField, requester);
const { error } = await this.validateAndGenerateFieldEntry({ ...this.fieldTypeConverter([searchField])[0], dataType: fieldInput.dataType }, requester);
if (error.length !== 0) {
throw new GraphQLError(JSON.stringify(error), { extensions: { code: errorCodes.CLIENT_MALFORMED_INPUT } });
}
Expand Down
1 change: 1 addition & 0 deletions packages/itmat-cores/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export * from './trpcCore/driveCore';
export * from './trpcCore/fileCore';
export * from './trpcCore/studyCore';
export * from './trpcCore/userCore';
export * from './trpcCore/permissionCore';
export * from './rest/fileDownload';
export * from './authentication/pubkeyAuthentication';
export * from './log/logPlugin';
Expand Down
2 changes: 1 addition & 1 deletion packages/itmat-cores/src/trpcCore/fileCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export class TRPCFileCore {
* @param studyId - The id of the study. Could be null for non-study files.
* @param userId - The id of the user.
* @param fileUpload - The file to upload.
* @param description - The description of the file.
* @param fileType - The type of the file.
* @param fileCategory - The category of the file.
* @param description - The description of the file.
* @param properties - The properties of the file. Note if the data is attached to a field, the fieldproperties will be used.
*
* @return IFile - The object of IFile.
Expand Down
21 changes: 21 additions & 0 deletions packages/itmat-cores/src/trpcCore/permissionCore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IUserWithoutToken } from '@itmat-broker/itmat-types';
import { DBType } from '../database/database';

export class TRPCPermissionCore {
db: DBType;
constructor(db: DBType) {
this.db = db;
}

/**
* Get the roles of a user.
*
* @param user
* @param studyId
* @returns
*/
public async getRolesOfUser(user: IUserWithoutToken, studyId?: string) {
return studyId ? await this.db.collections.roles_collection.find({ 'studyId': studyId, 'users': user.id, 'life.deletedTime': null }).toArray() :
await this.db.collections.roles_collection.find({ 'users': user.id, 'life.deletedTime': null }).toArray();
}
}
Loading

0 comments on commit 1e5e0c7

Please sign in to comment.