-
Notifications
You must be signed in to change notification settings - Fork 331
feat(operations): add read-only cases API foundation #386
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
Open
Mix13131
wants to merge
1
commit into
Worklenz:main
Choose a base branch
from
Mix13131:feat/operations-cases-foundation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
worklenz-backend/database/migrations/20260615000000-operational-cases.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| -- Minimal read-only operations foundation for cases used by operations views. | ||
|
|
||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'operational_case_type') THEN | ||
| CREATE TYPE operational_case_type AS ENUM ( | ||
| 'supplier_order', | ||
| 'repair', | ||
| 'shipment', | ||
| 'general' | ||
| ); | ||
| END IF; | ||
|
|
||
| IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'operational_case_status') THEN | ||
| CREATE TYPE operational_case_status AS ENUM ( | ||
| 'new', | ||
| 'waiting_internal', | ||
| 'waiting_external', | ||
| 'in_progress', | ||
| 'at_risk', | ||
| 'overdue', | ||
| 'done', | ||
| 'closed_problem' | ||
| ); | ||
| END IF; | ||
| END | ||
| $$; | ||
|
|
||
| CREATE OR REPLACE PROCEDURE pg_temp.add_constraint(_table TEXT, _constraint TEXT, _definition TEXT) | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = _constraint) THEN | ||
| EXECUTE FORMAT('ALTER TABLE %I ADD CONSTRAINT %I %s', _table, _constraint, _definition); | ||
| END IF; | ||
| END | ||
| $$; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS counterparties ( | ||
| id UUID DEFAULT uuid_generate_v4() NOT NULL, | ||
| team_id UUID NOT NULL, | ||
| name TEXT NOT NULL, | ||
| type TEXT NOT NULL, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
| updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL | ||
| ); | ||
|
|
||
| CALL pg_temp.add_constraint('counterparties', 'counterparties_pk', 'PRIMARY KEY (id)'); | ||
| CALL pg_temp.add_constraint('counterparties', 'counterparties_team_id_fk', 'FOREIGN KEY (team_id) REFERENCES teams ON DELETE CASCADE'); | ||
| CALL pg_temp.add_constraint('counterparties', 'counterparties_type_check', 'CHECK (type = ANY (ARRAY [''supplier''::TEXT, ''broker''::TEXT, ''repair_service''::TEXT, ''carrier''::TEXT, ''customer''::TEXT, ''internal''::TEXT, ''other''::TEXT]))'); | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS counterparties_name_type_team_uindex | ||
| ON counterparties (LOWER(name), type, team_id); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS assets ( | ||
| id UUID DEFAULT uuid_generate_v4() NOT NULL, | ||
| team_id UUID NOT NULL, | ||
| name TEXT NOT NULL, | ||
| inventory_no TEXT, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
| updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL | ||
| ); | ||
|
|
||
| CALL pg_temp.add_constraint('assets', 'assets_pk', 'PRIMARY KEY (id)'); | ||
| CALL pg_temp.add_constraint('assets', 'assets_team_id_fk', 'FOREIGN KEY (team_id) REFERENCES teams ON DELETE CASCADE'); | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS assets_inventory_no_team_uindex | ||
| ON assets (team_id, inventory_no) | ||
| WHERE inventory_no IS NOT NULL; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS operational_cases ( | ||
| id UUID DEFAULT uuid_generate_v4() NOT NULL, | ||
| team_id UUID NOT NULL, | ||
| project_id UUID, | ||
| title TEXT NOT NULL, | ||
| description TEXT, | ||
| case_type operational_case_type NOT NULL, | ||
| status operational_case_status DEFAULT 'new' NOT NULL, | ||
| owner_id UUID NOT NULL, | ||
| counterparty_id UUID, | ||
| asset_id UUID, | ||
| due_date TIMESTAMP WITH TIME ZONE, | ||
| external_wait_since TIMESTAMP WITH TIME ZONE, | ||
| last_activity_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
| priority_score NUMERIC DEFAULT 0 NOT NULL, | ||
| risk_score NUMERIC DEFAULT 0 NOT NULL, | ||
| no_deadline_reason TEXT, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
| updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL | ||
| ); | ||
|
|
||
| CALL pg_temp.add_constraint('operational_cases', 'operational_cases_pk', 'PRIMARY KEY (id)'); | ||
| CALL pg_temp.add_constraint('operational_cases', 'operational_cases_team_id_fk', 'FOREIGN KEY (team_id) REFERENCES teams ON DELETE CASCADE'); | ||
| CALL pg_temp.add_constraint('operational_cases', 'operational_cases_project_id_fk', 'FOREIGN KEY (project_id) REFERENCES projects ON DELETE SET NULL'); | ||
| CALL pg_temp.add_constraint('operational_cases', 'operational_cases_owner_id_fk', 'FOREIGN KEY (owner_id) REFERENCES users ON DELETE RESTRICT'); | ||
| CALL pg_temp.add_constraint('operational_cases', 'operational_cases_counterparty_id_fk', 'FOREIGN KEY (counterparty_id) REFERENCES counterparties ON DELETE SET NULL'); | ||
| CALL pg_temp.add_constraint('operational_cases', 'operational_cases_asset_id_fk', 'FOREIGN KEY (asset_id) REFERENCES assets ON DELETE SET NULL'); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS operational_cases_team_status_index | ||
| ON operational_cases (team_id, status); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS operational_cases_team_due_date_index | ||
| ON operational_cases (team_id, due_date); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS operational_cases_team_type_index | ||
| ON operational_cases (team_id, case_type); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS money_impacts ( | ||
| id UUID DEFAULT uuid_generate_v4() NOT NULL, | ||
| case_id UUID NOT NULL, | ||
| currency TEXT DEFAULT 'RUB'::TEXT NOT NULL, | ||
| object_value NUMERIC, | ||
| service_cost NUMERIC, | ||
| potential_loss NUMERIC, | ||
| downtime_cost_per_day NUMERIC, | ||
| delay_cost_per_day NUMERIC, | ||
| created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
| updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL | ||
| ); | ||
|
|
||
| CALL pg_temp.add_constraint('money_impacts', 'money_impacts_pk', 'PRIMARY KEY (id)'); | ||
| CALL pg_temp.add_constraint('money_impacts', 'money_impacts_case_id_fk', 'FOREIGN KEY (case_id) REFERENCES operational_cases ON DELETE CASCADE'); | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS money_impacts_case_id_uindex | ||
| ON money_impacts (case_id); | ||
6 changes: 6 additions & 0 deletions
6
worklenz-backend/database/migrations/20260616000000-operational-case-next-action.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ALTER TABLE operational_cases | ||
| ADD COLUMN IF NOT EXISTS next_action_text TEXT, | ||
| ADD COLUMN IF NOT EXISTS next_action_due_date TIMESTAMP WITH TIME ZONE; | ||
|
|
||
| CREATE INDEX IF NOT EXISTS operational_cases_team_next_action_due_date_index | ||
| ON operational_cases (team_id, next_action_due_date); |
6 changes: 6 additions & 0 deletions
6
worklenz-backend/database/migrations/20260619000000-operational-order-number.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ALTER TABLE operational_cases | ||
| ADD COLUMN IF NOT EXISTS order_number TEXT; | ||
|
|
||
| CREATE INDEX IF NOT EXISTS operational_cases_team_order_number_index | ||
| ON operational_cases (team_id, LOWER(order_number)) | ||
| WHERE order_number IS NOT NULL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import db from "../config/db"; | ||
| import HandleExceptions from "../decorators/handle-exceptions"; | ||
| import {IWorkLenzRequest} from "../interfaces/worklenz-request"; | ||
| import {IWorkLenzResponse} from "../interfaces/worklenz-response"; | ||
| import {ServerResponse} from "../models/server-response"; | ||
| import WorklenzControllerBase from "./worklenz-controller-base"; | ||
|
|
||
| const CASE_TYPE_FILTERS = new Set(["supplier_order", "repair", "shipment", "general"]); | ||
| const CASE_STATUS_FILTERS = new Set([ | ||
| "new", | ||
| "waiting_internal", | ||
| "waiting_external", | ||
| "in_progress", | ||
| "at_risk", | ||
| "overdue", | ||
| "done", | ||
| "closed_problem" | ||
| ]); | ||
|
|
||
| export default class CasesController extends WorklenzControllerBase { | ||
| @HandleExceptions() | ||
| public static async get(req: IWorkLenzRequest, res: IWorkLenzResponse): Promise<IWorkLenzResponse> { | ||
| const { | ||
| searchQuery, | ||
| searchParams, | ||
| sortField, | ||
| sortOrder, | ||
| size, | ||
| offset | ||
| } = this.toPaginationOptions(req.query, ["oc.title", "oc.description", "oc.order_number"], false, 4); | ||
| const allowedSortFields: Record<string, string> = { | ||
| title: "oc.title", | ||
| due_date: "oc.due_date", | ||
| status: "oc.status", | ||
| case_type: "oc.case_type", | ||
| priority_score: "oc.priority_score", | ||
| risk_score: "oc.risk_score", | ||
| updated_at: "oc.updated_at", | ||
| created_at: "oc.created_at" | ||
| }; | ||
| const orderBy = allowedSortFields[String(sortField)] || "oc.updated_at"; | ||
| const queryParams: unknown[] = [req.user?.team_id, size, offset, ...searchParams]; | ||
| let filters = searchQuery; | ||
|
|
||
| if (typeof req.query.case_type === "string" && CASE_TYPE_FILTERS.has(req.query.case_type)) { | ||
| queryParams.push(req.query.case_type); | ||
| filters += ` AND oc.case_type = $${queryParams.length}`; | ||
| } | ||
|
|
||
| if (typeof req.query.status === "string" && CASE_STATUS_FILTERS.has(req.query.status)) { | ||
| queryParams.push(req.query.status); | ||
| filters += ` AND oc.status = $${queryParams.length}`; | ||
| } | ||
|
|
||
| const q = ` | ||
| SELECT ROW_TO_JSON(rec) AS cases | ||
| FROM ( | ||
| SELECT COUNT(*) AS total, | ||
| ( | ||
| SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(ROW_TO_JSON(t))), '[]'::JSON) | ||
| FROM ( | ||
| SELECT | ||
| oc.id, | ||
| oc.team_id, | ||
| oc.title, | ||
| oc.order_number, | ||
| oc.description, | ||
| oc.case_type, | ||
| oc.status, | ||
| p.name AS project_name, | ||
| cp.name AS counterparty_name, | ||
| cp.type AS counterparty_type, | ||
| a.name AS asset_name, | ||
| a.inventory_no AS asset_inventory_no, | ||
| oc.due_date, | ||
| oc.next_action_text, | ||
| oc.next_action_due_date, | ||
| oc.external_wait_since, | ||
| oc.last_activity_at, | ||
| oc.priority_score, | ||
| oc.risk_score, | ||
| oc.no_deadline_reason, | ||
| oc.created_at, | ||
| oc.updated_at, | ||
| COALESCE(mi.currency, 'RUB') AS money_currency, | ||
| COALESCE(mi.object_value, 0) | ||
| + COALESCE(mi.service_cost, 0) | ||
| + COALESCE(mi.potential_loss, 0) AS money_impact_total, | ||
| jsonb_strip_nulls( | ||
| jsonb_build_object( | ||
| 'currency', mi.currency, | ||
| 'object_value', mi.object_value, | ||
| 'service_cost', mi.service_cost, | ||
| 'potential_loss', mi.potential_loss, | ||
| 'downtime_cost_per_day', mi.downtime_cost_per_day, | ||
| 'delay_cost_per_day', mi.delay_cost_per_day | ||
| ) | ||
| ) AS money_impact | ||
| FROM operational_cases oc | ||
| LEFT JOIN projects p ON p.id = oc.project_id AND p.team_id = oc.team_id | ||
| LEFT JOIN counterparties cp ON cp.id = oc.counterparty_id AND cp.team_id = oc.team_id | ||
| LEFT JOIN assets a ON a.id = oc.asset_id AND a.team_id = oc.team_id | ||
| LEFT JOIN money_impacts mi ON mi.case_id = oc.id | ||
| WHERE oc.team_id = $1 ${filters} | ||
| ORDER BY ${orderBy} ${sortOrder} | ||
| LIMIT $2 OFFSET $3 | ||
|
Comment on lines
+105
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Add a deterministic tie-breaker to paginated sorting. At Line 105-106, offset pagination sorts only by the selected field. When many rows share that value, page boundaries can shift and return duplicates/missing records. Suggested fix- ORDER BY ${orderBy} ${sortOrder}
+ ORDER BY ${orderBy} ${sortOrder}, oc.id ${sortOrder}
LIMIT $2 OFFSET $3🤖 Prompt for AI Agents |
||
| ) t | ||
| ) AS data | ||
| FROM operational_cases oc | ||
| WHERE oc.team_id = $1 ${filters} | ||
| ) rec; | ||
| `; | ||
| const result = await db.query(q, queryParams); | ||
| const [data] = result.rows; | ||
| return res.status(200).send(new ServerResponse(true, data?.cases || this.paginatedDatasetDefaultStruct)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import express from "express"; | ||
|
|
||
| import CasesController from "../../controllers/cases-controller"; | ||
| import safeControllerFunction from "../../shared/safe-controller-function"; | ||
|
|
||
| const casesApiRouter = express.Router(); | ||
|
|
||
| casesApiRouter.get("/", safeControllerFunction(CasesController.get)); | ||
|
|
||
| export default casesApiRouter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
worklenz-frontend/src/api/operations/operations.api.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { API_BASE_URL } from '@/shared/constants'; | ||
| import { IServerResponse } from '@/types/common.types'; | ||
| import { | ||
| IOperationalCasesResponse, | ||
| OperationalCaseStatus, | ||
| OperationalCaseType, | ||
| } from '@/types/operations/operations.types'; | ||
| import apiClient from '../api-client'; | ||
|
|
||
| const rootUrl = `${API_BASE_URL}/cases`; | ||
|
|
||
| export const operationsApiService = { | ||
| getCases: async (params?: { | ||
| case_type?: OperationalCaseType | 'all'; | ||
| status?: OperationalCaseStatus | 'all'; | ||
| search?: string; | ||
| }): Promise<IServerResponse<IOperationalCasesResponse>> => { | ||
| const searchParams = new URLSearchParams(); | ||
|
|
||
| if (params?.case_type && params.case_type !== 'all') { | ||
| searchParams.set('case_type', params.case_type); | ||
| } | ||
| if (params?.status && params.status !== 'all') { | ||
| searchParams.set('status', params.status); | ||
| } | ||
| if (params?.search) { | ||
| searchParams.set('search', params.search); | ||
| } | ||
|
|
||
| const query = searchParams.toString(); | ||
| const response = await apiClient.get<IServerResponse<IOperationalCasesResponse>>( | ||
| `${rootUrl}${query ? `?${query}` : ''}` | ||
| ); | ||
| return response.data; | ||
| }, | ||
| }; |
53 changes: 53 additions & 0 deletions
53
worklenz-frontend/src/types/operations/operations.types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| export type OperationalCaseType = 'supplier_order' | 'repair' | 'shipment' | 'general'; | ||
|
|
||
| export type OperationalCaseStatus = | ||
| | 'new' | ||
| | 'waiting_internal' | ||
| | 'waiting_external' | ||
| | 'in_progress' | ||
| | 'at_risk' | ||
| | 'overdue' | ||
| | 'done' | ||
| | 'closed_problem'; | ||
|
|
||
| export interface IMoneyImpact { | ||
| currency?: string | null; | ||
| object_value?: number | null; | ||
| service_cost?: number | null; | ||
| potential_loss?: number | null; | ||
| downtime_cost_per_day?: number | null; | ||
| delay_cost_per_day?: number | null; | ||
| } | ||
|
|
||
| export interface IOperationalCase { | ||
| id: string; | ||
| team_id?: string; | ||
| title: string; | ||
| order_number?: string | null; | ||
| description?: string | null; | ||
| case_type: OperationalCaseType; | ||
| status: OperationalCaseStatus; | ||
| project_name?: string | null; | ||
| counterparty_name?: string | null; | ||
| counterparty_type?: string | null; | ||
| asset_name?: string | null; | ||
| asset_inventory_no?: string | null; | ||
| due_date?: string | null; | ||
| next_action_text?: string | null; | ||
| next_action_due_date?: string | null; | ||
| external_wait_since?: string | null; | ||
| last_activity_at?: string | null; | ||
| priority_score?: number | null; | ||
| risk_score?: number | null; | ||
| no_deadline_reason?: string | null; | ||
| money_currency?: string | null; | ||
| money_impact?: IMoneyImpact; | ||
| money_impact_total?: number | null; | ||
| created_at?: string | null; | ||
| updated_at?: string | null; | ||
| } | ||
|
|
||
| export interface IOperationalCasesResponse { | ||
| total: number; | ||
| data: IOperationalCase[]; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Enforce tenant-safe foreign keys for related entities.
At Line 96-97,
counterparty_idandasset_idonly referenceid, so a case can point to another team’s records. The read query later joins on bothidandteam_id(worklenz-backend/src/controllers/cases-controller.tsLine 101-103), which hides the mismatch as nulls instead of preventing bad writes.Suggested schema fix
🤖 Prompt for AI Agents