Skip to content

Commit

Permalink
Merge pull request #1511 from Tampere/feature/Limit-project-object-ty…
Browse files Browse the repository at this point in the history
…pes-to-one-per-object

Limit project object type to one
  • Loading branch information
AttePeramaki authored Feb 25, 2025
2 parents 4fcbee3 + 99560a6 commit edc1973
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 26 deletions.
11 changes: 3 additions & 8 deletions backend/src/components/projectObject/investment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getProjectObjectFragment = (ids: string | string[]) => sql.fragment`
sap_wbs_id AS "sapWBSId",
dump.geom,
dump.geometry_dump AS "geometryDump",
(SELECT json_agg((object_type).id)
(SELECT json_agg((object_type).id)->0
FROM app.project_object_type
WHERE project_object.id = project_object_type.project_object_id) AS "objectType",
(SELECT json_agg((object_category).id)
Expand Down Expand Up @@ -81,22 +81,17 @@ async function updateObjectTypes(
tx: DatabaseTransactionConnection,
projectObject: UpdateInvestmentProjectObject,
) {
if (!Array.isArray(projectObject.objectType)) {
if (!projectObject.objectType) {
return;
}

await tx.query(sql.untyped`
DELETE FROM app.project_object_type WHERE project_object_id = ${projectObject.projectObjectId}
`);

const tuples = projectObject.objectType.map((type) => [projectObject.projectObjectId, type]);

await tx.any(sql.untyped`
INSERT INTO app.project_object_type (project_object_id, object_type)
SELECT
t.project_object_id,
('KohdeTyyppi', t.object_type)::app.code_id
FROM ${sql.unnest(tuples, ['uuid', 'text'])} AS t (project_object_id, object_type);
VALUES (${projectObject.projectObjectId}, ('KohdeTyyppi', ${projectObject.objectType})::app.code_id);
`);
}

Expand Down
3 changes: 2 additions & 1 deletion backend/src/components/taskQueue/workTableReportQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export async function setupWorkTableReportQueue() {
projectEndDate: (row) => dayjs(row.projectDateRange.endDate).format(reportDateFormat),
objectStartDate: (row) => dayjs(row.objectDateRange.startDate).format(reportDateFormat),
objectEndDate: (row) => dayjs(row.objectDateRange.endDate).format(reportDateFormat),
objectType: (row) => formatIdArrayToText(row.objectType, 'objectType'),
objectType: (row) =>
codes.objectType.find((code) => row.objectType === code.id.id)?.text['fi'],
objectCategory: (row) => formatIdArrayToText(row.objectCategory, 'objectCategory'),
objectUsage: (row) => formatIdArrayToText(row.objectUsage, 'objectUsage'),
committee: (row) => formatIdArrayToText([row.committee], 'committee'),
Expand Down
2 changes: 1 addition & 1 deletion e2e/test/api/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const testProjectObject = (projectId: string, committees: string[], user: User)
objectStage: '01',
lifecycleState: '01',
committee: committees[0],
objectType: ['01'],
objectType: '01',
objectCategory: ['01'],
objectUsage: ['01'],
startDate: '2021-01-01',
Expand Down
8 changes: 4 additions & 4 deletions e2e/test/api/projectObjectData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const testProjectObject = (
objectName: 'Test project object',
objectStage: '01',
lifecycleState: '01',
objectType: ['01'],
objectType: '01',
objectCategory: ['01'],
objectUsage: ['01'],
committee: projectCommittees[0],
Expand Down Expand Up @@ -80,7 +80,7 @@ export const testProjectObject2 = (
landownership: null,
locationOnProperty: null,
height: null,
objectType: ['01'],
objectType: '01',
objectStage: '01',
}
: { contract: 'contract n.1', procurementMethod: '01', poNumber: '123456' }),
Expand Down Expand Up @@ -110,7 +110,7 @@ export const testProjectObject3 = (
landownership: null,
locationOnProperty: null,
height: null,
objectType: ['01'],
objectType: '01',
objectStage: '01',
}
: { contract: 'contract n.1', procurementMethod: '01', poNumber: '123456' }),
Expand Down Expand Up @@ -140,7 +140,7 @@ export const invalidDateProjectObject = (
landownership: null,
locationOnProperty: null,
height: null,
objectType: ['01'],
objectType: '01',
objectStage: '01',
}
: { contract: 'contract n.1', procurementMethod: '01', poNumber: '123456' }),
Expand Down
4 changes: 2 additions & 2 deletions e2e/test/api/projectPermissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test.describe('permission testing', () => {
description: 'Test project object description',
committee: project.committees[0],
lifecycleState: '01',
objectType: ['01'],
objectType: '01',
objectCategory: ['01'],
objectUsage: ['01'],
objectStage: '01',
Expand Down Expand Up @@ -142,7 +142,7 @@ test.describe('permission testing', () => {
committee: project.committees[0],
lifecycleState: '01',
objectStage: '01',
objectType: ['01'],
objectType: '01',
objectCategory: ['01'],
objectUsage: ['01'],
startDate: '2021-01-01',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,7 @@ export const InvestmentProjectObjectForm = forwardRef(function InvestmentProject
errorTooltip={tr('projectObject.objectTypeTooltip')}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
component={({ ref, ...field }) => (
<CodeSelect
{...field}
multiple
codeListId="KohdeTyyppi"
readOnly={!editing}
maxTags={3}
/>
<CodeSelect {...field} codeListId="KohdeTyyppi" readOnly={!editing} />
)}
/>

Expand Down
2 changes: 1 addition & 1 deletion shared/src/schema/projectObject/investment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { commonDbProjectObjectSchema, newProjectObjectSchema } from './base.js';

export const newInvestmentProjectObjectSchema = newProjectObjectSchema.extend({
objectStage: codeId,
objectType: z.array(codeId).superRefine((value) => value.length > 0),
objectType: codeId,
committee: nonEmptyString,
});

Expand Down
2 changes: 1 addition & 1 deletion shared/src/schema/projectObject/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const projectObjectSearchSchema = z.object({
dateRange: periodSchema.optional(),
map: mapSearchSchema.optional(),
objectParticipantUser: nonEmptyString.nullish(),
objectTypes: mergedProjectObjectDbSchema.shape.objectType.optional(),
objectTypes: z.array(mergedProjectObjectDbSchema.shape.objectType).optional(),
objectCategories: mergedProjectObjectDbSchema.shape.objectCategory.optional(),
objectUsages: mergedProjectObjectDbSchema.shape.objectUsage.optional(),
lifecycleStates: z.array(mergedProjectObjectDbSchema.shape.lifecycleState).optional(),
Expand Down
2 changes: 1 addition & 1 deletion shared/src/schema/workTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const workTableSearchSchema = z.object({
projectObjectName: z.string().optional(),
objectStartDate: dbInvestmentProjectObjectSchema.shape.startDate.optional().nullable(),
objectEndDate: dbInvestmentProjectObjectSchema.shape.endDate.optional().nullable(),
objectType: dbInvestmentProjectObjectSchema.shape.objectType.optional(),
objectType: z.array(dbInvestmentProjectObjectSchema.shape.objectType).optional(),
objectCategory: dbInvestmentProjectObjectSchema.shape.objectCategory.optional(),
objectUsage: dbInvestmentProjectObjectSchema.shape.objectUsage.optional(),
lifecycleState: z.array(dbInvestmentProjectObjectSchema.shape.lifecycleState).optional(),
Expand Down

0 comments on commit edc1973

Please sign in to comment.