Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak(types): RN-1418: Update EntityType type #5871

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/
import { EntityType, QuestionType } from '@tupaia/types';
import { QuestionType } from '@tupaia/types';
import { getUniqueSurveyQuestionFileName } from '@tupaia/utils';
import { generateId } from '@tupaia/database';
import { processSurveyResponse } from '../routes/SubmitSurveyReponse/processSurveyResponse';
Expand All @@ -12,7 +12,7 @@ const mockFindEntityById = async (id: string) => ({
id: 'theEntityId',
code: 'theEntityCode',
name: 'The Entity Name',
type: 'facility' as EntityType,
type: 'facility',
});

const optionSetId = 'optionSetId';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { Route } from '@tupaia/server-boilerplate';
import { DatatrakWebEntityDescendantsRequest, EntityType, UserAccount } from '@tupaia/types';
import { DatatrakWebEntityDescendantsRequest, UserAccount } from '@tupaia/types';
import { TupaiaApiClient } from '@tupaia/api-client';
import { Request } from 'express';
import camelcaseKeys from 'camelcase-keys';
Expand Down Expand Up @@ -45,7 +45,7 @@ const getRecentEntities = (

const entityTypes = type.split(',');
const recentEntitiesOfTypes = entityTypes
.map(entityType => userRecentEntities[countryCode][entityType as EntityType] ?? [])
.map(entityType => userRecentEntities[countryCode][entityType] ?? [])
.flat();

return recentEntitiesOfTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, { Ref } from 'react';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { screen } from '@testing-library/react';
import { EntityType } from '@tupaia/types';
import { EntityTypeEnum } from '@tupaia/types';
import { spyOnMockRequest } from '../../helpers/spyOnMockRequest';
import { renderComponent } from '../../helpers/render';
import { EntityQuestion } from '../../../features/Questions';
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('Entity Question', () => {
config={{
entity: {
filter: {
type: EntityType.facility,
type: EntityTypeEnum.facility,
parentId: {
questionId: 'theParentQuestionId',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { Request, NextFunction, Response } from 'express';
import { PermissionsError } from '@tupaia/utils';
import { ajvValidate, isNotNullish } from '@tupaia/tsutils';
import { EntityType } from '@tupaia/types';
import { EntityTypeEnum } from '@tupaia/types';
import { EntityRecord, EntityFilter } from '@tupaia/server-boilerplate';
import { MultiEntityRequestBody, MultiEntityRequestBodySchema } from '../types';
import { extractFilterFromQuery } from './filter';
Expand All @@ -30,7 +30,7 @@ const validateEntitiesAndBuildContext = async (
const { hierarchyName } = req.params;
// Root type shouldn't be locked into being a project entity, see: https://github.com/beyondessential/tupaia-backlog/issues/2570
const rootEntity = await req.models.entity.findOne({
type: EntityType.project,
type: EntityTypeEnum.project,
code: hierarchyName,
});
if (!rootEntity) {
Expand Down Expand Up @@ -161,7 +161,7 @@ export const attachEntityFilterContext = async (
next: NextFunction,
) => {
const rootEntity = await req.models.entity.findOne({
type: EntityType.project,
type: EntityTypeEnum.project,
code: req.params.hierarchyName,
});

Expand Down
3 changes: 2 additions & 1 deletion packages/types/config/models/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"MapOverlayConfig": "./models-extra",
"EntityAttributes": "./models-extra",
"UserAccountPreferences": "./models-extra",
"ProjectConfig": "./models-extra"
"ProjectConfig": "./models-extra",
"EntityType": "./models-extra"
}
}
}
2 changes: 1 addition & 1 deletion packages/types/config/models/template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export enum {{convertedName}} {
{{/inline}}
/*
* Tupaia
* Copyright (c) 2017 - 2020 Beyond Essential Systems Pty Ltd
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*
*/
/*
Expand Down
18 changes: 17 additions & 1 deletion packages/types/generate-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,28 @@ const removeUnwantedColumns = (tables: Table[]) =>
columns: columns.filter(({ name }) => !name.includes('m_row$')), // Remove mvrefresh columns
}));

/**
* @description There is currently no way to rename just one enum definition in the sql-ts library, so we have to do it manually. This is so that we can set columns of type entity_type to be EntityTypeEnum + string
*/
const renameEntityTypeEnumDefinition = (enums: any) => {
return enums.map((enumDef: any) => {
if (enumDef.name === 'entity_type') {
return {
...enumDef,
convertedName: 'EntityTypeEnum',
};
}
return enumDef;
});
};
const run = async () => {
const failOnChanges = process.argv[2] === '--failOnChanges';

// @ts-ignore
const definitions = await sqlts.toObject(config, db);

const enums = renameEntityTypeEnumDefinition(definitions.enums);

// Base Model tables should mark columns with defaultValues as non-optional since the default value will be present
const baseTables = makeDefaultColumnsRequired(definitions.tables);

Expand All @@ -88,7 +104,7 @@ const run = async () => {

const allTables = removeUnwantedColumns(combineTables(baseTables, createTables, updateTables));

const tsString = sqlts.fromObject({ ...definitions, tables: allTables }, config);
const tsString = sqlts.fromObject({ ...definitions, enums, tables: allTables }, config);

if (failOnChanges) {
const currentTsString = fs.readFileSync(config.filename, { encoding: 'utf8' });
Expand Down
Loading