Skip to content

Commit

Permalink
rename vantage to vantage_mode_category
Browse files Browse the repository at this point in the history
  • Loading branch information
mauberti-bc committed Dec 14, 2024
1 parent 5783b3b commit 493e6bd
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 39 deletions.
2 changes: 1 addition & 1 deletion api/src/database-models/vantage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { z } from 'zod';
* @description Data model for `vantage`.
*/
export const VantageModel = z.object({
vantage_id: z.number(),
vantage_mode_category_id: z.number(),
name: z.string(),
description: z.string().nullable(),
record_end_date: z.string().nullable(),
Expand Down
2 changes: 1 addition & 1 deletion api/src/database-models/vantage_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { z } from 'zod';
*/
export const VantageModeModel = z.object({
vantage_mode_id: z.number(),
vantage_id: z.number(),
vantage_mode_category_id: z.number(),
name: z.string(),
description: z.string().nullable(),
record_end_date: z.string().nullable(),
Expand Down
12 changes: 6 additions & 6 deletions api/src/openapi/schemas/technique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const techniqueVantagesSchema: OpenAPIV3.SchemaObject = {
description: 'Vantages from which a method is done, like water, air, or ground.',
items: {
type: 'object',
required: ['vantage_mode_method_id', 'vantage_id'],
required: ['vantage_mode_method_id', 'vantage_mode_category_id'],
additionalProperties: false,
properties: {
method_technique_vantage_mode_id: {
Expand All @@ -94,7 +94,7 @@ export const techniqueVantagesSchema: OpenAPIV3.SchemaObject = {
type: 'integer',
minimum: 1
},
vantage_id: {
vantage_mode_category_id: {
type: 'integer',
minimum: 1
}
Expand Down Expand Up @@ -192,10 +192,10 @@ export const vantageReferenceRecordsSchema: OpenAPIV3.SchemaObject = {
items: {
type: 'object',
description: 'Vantage reference record and its associated vantage modes.',
required: ['vantage_id', 'name', 'description', 'vantage_modes'],
required: ['vantage_mode_category_id', 'name', 'description', 'vantage_modes'],
additionalProperties: false,
properties: {
vantage_id: {
vantage_mode_category_id: {
type: 'integer',
minimum: 1
},
Expand All @@ -211,14 +211,14 @@ export const vantageReferenceRecordsSchema: OpenAPIV3.SchemaObject = {
description: 'Supported vantage mode for the vantage record.',
items: {
type: 'object',
required: ['vantage_mode_method_id', 'name', 'vantage_id', 'description'],
required: ['vantage_mode_method_id', 'name', 'vantage_mode_category_id', 'description'],
additionalProperties: false,
properties: {
vantage_mode_method_id: {
type: 'integer',
description: 'The primary key of the vantage mode option.'
},
vantage_id: {
vantage_mode_category_id: {
type: 'integer',
description: 'The vantage of the mode.'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ describe('getTechniques', () => {
{
method_technique_vantage_mode_id: 1,
vantage_mode_method_id: 101,
vantage_id: 2
vantage_mode_category_id: 2
},
{
method_technique_vantage_mode_id: 2,
vantage_mode_method_id: 102,
vantage_id: 1
vantage_mode_category_id: 1
}
]
};
Expand Down Expand Up @@ -258,12 +258,12 @@ describe('getTechniques', () => {
{
method_technique_vantage_mode_id: 1,
vantage_mode_method_id: 101,
vantage_id: 2
vantage_mode_category_id: 2
},
{
method_technique_vantage_mode_id: 2,
vantage_mode_method_id: 102,
vantage_id: 1
vantage_mode_category_id: 1
}
]
};
Expand Down
4 changes: 2 additions & 2 deletions api/src/paths/reference/get/vantage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ describe('getVantageModes', () => {
it('should return vantage modes for method lookup ids', async () => {
const mockVantageModeResponse = [
{
vantage_id: 101,
vantage_mode_category_id: 101,
name: 'Vantage A',
description: 'Description for vantage A',
vantage_modes: [{ vantage_mode_method_id: 1, name: 'Mode A', description: 'Description' }]
},
{
vantage_id: 102,
vantage_mode_category_id: 102,
name: 'Vantage B',
description: 'Description for vantage B',
vantage_modes: [
Expand Down
2 changes: 1 addition & 1 deletion api/src/repositories/code-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export class CodeRepository extends BaseRepository {
async getVantages() {
const sqlStatement = SQL`
SELECT
vantage_id AS id,
vantage_mode_category_id AS id,
name,
description
FROM vantage
Expand Down
2 changes: 1 addition & 1 deletion api/src/repositories/technique-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class TechniqueRepository extends BaseRepository {
json_agg(json_build_object(
'method_technique_vantage_mode_id', method_technique_vantage_mode.method_technique_vantage_mode_id,
'vantage_mode_method_id', method_technique_vantage_mode.vantage_mode_method_id,
'vantage_id', vantage_mode.vantage_id
'vantage_mode_category_id', vantage_mode.vantage_mode_category_id
)) as vantage_mode_methods
`)
)
Expand Down
4 changes: 2 additions & 2 deletions api/src/repositories/technique-vantage-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const defaultLog = getLogger('repositories/technique-vantage-repository');
export const TechniqueVantage = z.object({
method_technique_vantage_mode_id: z.number(),
vantage_mode_method_id: z.number(),
vantage_id: z.number()
vantage_mode_category_id: z.number()
});

export type TechniqueVantage = z.infer<typeof TechniqueVantage>;
Expand Down Expand Up @@ -38,7 +38,7 @@ export class TechniqueVantageRepository extends BaseRepository {
.select(
'method_technique_vantage_mode.method_technique_vantage_mode_id',
'method_technique_vantage_mode.vantage_mode_method_id',
'vantage_mode.vantage_id'
'vantage_mode.vantage_mode_category_id'
)
.from('method_technique_vantage_mode')
.join(
Expand Down
2 changes: 1 addition & 1 deletion api/src/repositories/vantage-mode-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('VantageModeRepository', () => {
describe('getVantageModesByMethodLookupIds', () => {
it('should successfully return vantage modes for provided method lookup ids', async () => {
const mockVantageMode: VantageReferenceRecord = {
vantage_id: 101,
vantage_mode_category_id: 101,
name: 'Vantage A',
description: 'Description for vantage A',
vantage_modes: [{ vantage_mode_method_id: 1, name: 'Mode A', description: 'Description' }]
Expand Down
8 changes: 4 additions & 4 deletions api/src/repositories/vantage-mode-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ export class VantageModeRepository extends BaseRepository {

const queryBuilder = knex
.select(
'v.vantage_id',
'v.vantage_mode_category_id',
'v.name',
'v.description',
knex.raw(`
json_agg(
json_build_object(
'vantage_mode_method_id', vmm.vantage_mode_method_id,
'vantage_id', vm.vantage_id,
'vantage_mode_category_id', vm.vantage_mode_category_id,
'name', vm.name,
'description', vm.description
)
Expand All @@ -97,10 +97,10 @@ export class VantageModeRepository extends BaseRepository {
)
.from('vantage_mode_method as vmm')
.join('vantage_mode as vm', 'vmm.vantage_mode_id', 'vm.vantage_mode_id')
.join('vantage as v', 'v.vantage_id', 'vm.vantage_id')
.join('vantage as v', 'v.vantage_mode_category_id', 'vm.vantage_mode_category_id')
.whereIn('vmm.method_lookup_id', methodLookupIds)
.whereNull('vmm.record_end_date')
.groupBy('v.vantage_id', 'v.name', 'v.description');
.groupBy('v.vantage_mode_category_id', 'v.name', 'v.description');

const response = await this.connection.knex(queryBuilder, VantageReferenceRecord);

Expand Down
6 changes: 3 additions & 3 deletions api/src/services/technique-vantage-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('TechniqueVantageService', () => {
{
method_technique_vantage_mode_id: 2,
vantage_mode_method_id: 102,
vantage_id: 1
vantage_mode_category_id: 1
}
];

Expand Down Expand Up @@ -99,12 +99,12 @@ describe('TechniqueVantageService', () => {
{
method_technique_vantage_mode_id: 1,
vantage_mode_method_id: 101,
vantage_id: 2
vantage_mode_category_id: 2
},
{
method_technique_vantage_mode_id: 2,
vantage_mode_method_id: 102,
vantage_id: 1
vantage_mode_category_id: 1
}
];

Expand Down
2 changes: 1 addition & 1 deletion api/src/services/vantage-mode-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('VantageModeService', () => {
describe('getVantageReferenceRecordsByMethodLookupIds', () => {
it('should run successfully and return vantage modes for the provided method lookup ids', async () => {
const mockVantageMode: VantageReferenceRecord = {
vantage_id: 101,
vantage_mode_category_id: 101,
name: 'Vantage A',
description: 'Description for vantage A',
vantage_modes: [{ vantage_mode_method_id: 1, name: 'Mode A', description: 'Description' }]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type TechniqueAttributeFormValues =
};

export type TechniqueVantagesFormValues = {
vantage_id: number;
vantage_mode_category_id: number;
vantage_mode_method_id: number;
// internal ID used for form control keys. Not to be sent to API.
_id?: string;
Expand Down Expand Up @@ -97,7 +97,7 @@ const TechniqueFormContainer = <FormValues extends CreateTechniqueFormValues | U
vantage_mode_methods: yup.array(
yup.object().shape({
vantage_mode_method_id: yup.number().required('Vantage mode is required.'),
vantage_id: yup.number().required('Vantage is required.')
vantage_mode_category_id: yup.number().required('Vantage is required.')
})
)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const TechniqueVantageForm = <FormValues extends CreateTechniqueFormValue
const { values } = useFormikContext<FormValues>();

const getUnitOptions = (vantageModeMethodId: number, categoryId: number) => {
const selectedVantage = vantageReferenceRecords.find((record) => record.vantage_id === categoryId);
const selectedVantage = vantageReferenceRecords.find((record) => record.vantage_mode_category_id === categoryId);

if (!selectedVantage || !selectedVantage.vantage_modes) {
return [];
Expand Down Expand Up @@ -63,11 +63,11 @@ export const TechniqueVantageForm = <FormValues extends CreateTechniqueFormValue
<DualAutocompleteField
label="Vantage"
categoryOptions={vantageReferenceRecords.map((record) => ({
value: record.vantage_id,
value: record.vantage_mode_category_id,
label: record.name
}))}
getUnitOptions={(categoryId: number) => getUnitOptions(vantage.vantage_mode_method_id, categoryId)}
formikCategoryFieldName={`vantage_mode_methods.[${index}].vantage_id`}
formikCategoryFieldName={`vantage_mode_methods.[${index}].vantage_mode_category_id`}
formikUnitFieldName={`vantage_mode_methods.[${index}].vantage_mode_method_id`}
onDelete={() => arrayHelpers.remove(index)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const CreateTechniquePage = () => {
},
vantage_mode_methods: values.vantage_mode_methods.map((vantage_mode_method) => ({
vantage_mode_method_id: vantage_mode_method.vantage_mode_method_id,
vantage_id: vantage_mode_method.vantage_id
vantage_mode_category_id: vantage_mode_method.vantage_mode_category_id
}))
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const EditTechniquePage = () => {
],
vantage_mode_methods: technique.vantage_mode_methods.map((vantage_mode_method) => ({
_id: v4(), // A temporary unique id for react keys, etc, as the attribute_id is not unique
vantage_id: vantage_mode_method.vantage_id,
vantage_mode_category_id: vantage_mode_method.vantage_mode_category_id,
vantage_mode_method_id: vantage_mode_method.vantage_mode_method_id
}))
};
Expand Down Expand Up @@ -120,7 +120,7 @@ export const EditTechniquePage = () => {
},
vantage_mode_methods: values.vantage_mode_methods.map((vantage_mode_method) => ({
vantage_mode_method_id: vantage_mode_method.vantage_mode_method_id,
vantage_id: vantage_mode_method.vantage_id
vantage_mode_category_id: vantage_mode_method.vantage_mode_category_id
}))
};

Expand Down
4 changes: 2 additions & 2 deletions app/src/interfaces/useReferenceApi.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ export interface IGetTechniqueAttributes {
}

export type Vantage = {
vantage_id: number;
vantage_mode_category_id: number;
name: string;
description: string | null;
};

export type VantageMode = {
vantage_mode_method_id: number;
vantage_id: number;
vantage_mode_category_id: number;
name: string;
};

Expand Down
4 changes: 2 additions & 2 deletions app/src/interfaces/useTechniqueApi.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export type TechniqueQuantitativeAttribute = {
};

export type TechniqueVantageModeMethod = {
vantage_id: number;
vantage_mode_category_id: number;
vantage_mode_method_id: number;
};

type PostVantageMode = Omit<VantageMode, 'name' | 'vantage_id'>;
type PostVantageMode = Omit<VantageMode, 'name' | 'vantage_mode_category_id'>;

export interface ICreateTechniqueRequest {
name: string;
Expand Down
61 changes: 61 additions & 0 deletions database/src/migrations/20241213161700_rename_vantage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Knex } from 'knex';

/**
* Migration to rename the "vantage" table to "vantage_mode_category"
* and rename its primary key column "vantage_id" to "vantage_mode_category_id".
* It also updates the foreign key references in related tables using raw SQL.
*
* @param {Knex} knex
* @returns {Promise<void>}
*/
export async function up(knex: Knex): Promise<void> {
await knex.raw(`
DROP VIEW biohub_dapi_v1.vantage;
DROP VIEW biohub_dapi_v1.vantage_mode;
SET SEARCH_PATH = biohub;
----------------------------------------------------------------------------------------
-- RENAME VANTAGE TO VANTAGE_MODE_CATEGORY
----------------------------------------------------------------------------------------
ALTER TABLE vantage RENAME COLUMN vantage_id TO vantage_mode_category_id;
ALTER TABLE vantage RENAME TO vantage_mode_category;
ALTER TABLE vantage_mode RENAME COLUMN vantage_id TO vantage_mode_category_id;
----------------------------------------------------------------------------------------
-- UPDATE FOREIGN KEYS
----------------------------------------------------------------------------------------
ALTER TABLE vantage_mode DROP CONSTRAINT vantage_mode_fk1;
ALTER TABLE vantage_mode ADD CONSTRAINT vantage_mode_fk1
FOREIGN KEY (vantage_mode_category_id)
REFERENCES vantage_mode_category(vantage_mode_category_id);
----------------------------------------------------------------------------------------
-- UPDATE INDEXES
----------------------------------------------------------------------------------------
DROP INDEX IF EXISTS vantage_mode_idx1;
DROP INDEX IF EXISTS vantage_mode_nuk1;
-- indexes on vantage_mode
CREATE UNIQUE INDEX vantage_mode_nuk1 ON vantage_mode(vantage_mode_category_id, name, (record_end_date is NULL)) where record_end_date is null;
CREATE INDEX vantage_mode_idx1 ON vantage_mode(vantage_mode_category_id);
----------------------------------------------------------------------------------------
-- UPDATE VIEWS
----------------------------------------------------------------------------------------
SET SEARCH_PATH = biohub_dapi_v1;
CREATE OR REPLACE VIEW vantage_mode_category AS SELECT * FROM biohub.vantage_mode_category;
CREATE OR REPLACE VIEW vantage_mode AS SELECT * FROM biohub.vantage_mode;
`);
}

export async function down(knex: Knex): Promise<void> {
}

0 comments on commit 493e6bd

Please sign in to comment.