forked from ytgov/internal-data-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from icefoganalytics/issue-42/build-dataset-s…
…earch-feature Build Dataset Search Feature
- Loading branch information
Showing
26 changed files
with
1,076 additions
and
76 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
14 changes: 14 additions & 0 deletions
14
api/src/db/migrations/2024.06.10T17.08.32.add-acronym-to-user-groups.ts
This file contains 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,14 @@ | ||
import { DataTypes } from "sequelize" | ||
|
||
import type { Migration } from "@/db/umzug" | ||
|
||
export const up: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.addColumn("user_groups", "acronym", { | ||
type: DataTypes.STRING(10), | ||
allowNull: true, | ||
}) | ||
} | ||
|
||
export const down: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.removeColumn("user_groups", "acronym") | ||
} |
26 changes: 26 additions & 0 deletions
26
api/src/db/migrations/2024.06.10T18.01.34.backfill-user-groups-acronyms.ts
This file contains 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,26 @@ | ||
import { DataTypes } from "sequelize" | ||
|
||
import type { Migration } from "@/db/umzug" | ||
import { UserGroup } from "@/models" | ||
import acronymize from "@/utils/acronymize" | ||
|
||
export const up: Migration = async ({ context: queryInterface }) => { | ||
await UserGroup.findEach(async (userGroup) => { | ||
const acronym = acronymize(userGroup.name) | ||
await userGroup.update({ | ||
acronym, | ||
}) | ||
}) | ||
|
||
await queryInterface.changeColumn("user_groups", "acronym", { | ||
type: DataTypes.STRING(10), | ||
allowNull: false, | ||
}) | ||
} | ||
|
||
export const down: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.changeColumn("user_groups", "acronym", { | ||
type: DataTypes.STRING(10), | ||
allowNull: true, | ||
}) | ||
} |
This file contains 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
This file contains 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,78 @@ | ||
import { literal } from "sequelize" | ||
import { Literal } from "sequelize/types/utils" | ||
|
||
import { compactSql } from "@/utils/compact-sql" | ||
|
||
/** | ||
* Requires replacements to be passed in to query. | ||
* e.g. { replacements: { searchTokenWildcard: `%${searchToken}%`, searchToken } | ||
*/ | ||
export function datasetsSearch(): Literal { | ||
const matchingEntries = compactSql(/*sql*/ ` | ||
( | ||
SELECT | ||
DISTINCT datasets.id | ||
FROM | ||
datasets | ||
WHERE | ||
datasets.deleted_at IS NULL | ||
AND ( | ||
LOWER(datasets.name) LIKE LOWER(:searchTokenWildcard) | ||
OR LOWER(datasets.description) LIKE LOWER(:searchTokenWildcard) | ||
OR EXISTS ( | ||
SELECT | ||
1 | ||
FROM | ||
taggings | ||
INNER JOIN tags ON taggings.tag_id = tags.id | ||
AND datasets.id = taggings.taggable_id | ||
AND taggings.taggable_type = 'Dataset' | ||
AND tags.deleted_at IS NULL | ||
AND taggings.deleted_at IS NULL | ||
AND LOWER(tags.name) LIKE LOWER(:searchTokenWildcard) | ||
) | ||
OR EXISTS ( | ||
SELECT | ||
1 | ||
FROM | ||
user_groups | ||
INNER JOIN dataset_stewardships ON ( | ||
( | ||
dataset_stewardships.department_id = user_groups.id | ||
AND user_groups.type = 'department' | ||
) | ||
OR ( | ||
dataset_stewardships.department_id IS NOT NULL | ||
AND dataset_stewardships.division_id = user_groups.id | ||
AND user_groups.type = 'division' | ||
) | ||
OR ( | ||
dataset_stewardships.department_id IS NOT NULL | ||
AND dataset_stewardships.division_id IS NOT NULL | ||
AND dataset_stewardships.branch_id = user_groups.id | ||
AND user_groups.type = 'branch' | ||
) | ||
OR ( | ||
dataset_stewardships.department_id IS NOT NULL | ||
AND dataset_stewardships.division_id IS NOT NULL | ||
AND dataset_stewardships.branch_id IS NOT NULL | ||
AND dataset_stewardships.unit_id = user_groups.id | ||
AND user_groups.type = 'unit' | ||
) | ||
) | ||
AND datasets.id = dataset_stewardships.dataset_id | ||
AND dataset_stewardships.deleted_at IS NULL | ||
AND user_groups.deleted_at IS NULL | ||
AND ( | ||
LOWER(user_groups.name) LIKE LOWER(:searchTokenWildcard) | ||
OR user_groups.acronym = :searchToken | ||
) | ||
) | ||
) | ||
) | ||
`) | ||
|
||
return literal(matchingEntries) | ||
} | ||
|
||
export default datasetsSearch |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
File renamed without changes.
This file contains 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
This file contains 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,57 @@ | ||
import { DeepPartial } from "fishery" | ||
|
||
import { DatasetStewardship } from "@/models" | ||
import BaseFactory from "@/factories/base-factory" | ||
|
||
export const datasetStewardshipFactory = BaseFactory.define<DatasetStewardship>( | ||
({ sequence, params, onCreate }) => { | ||
onCreate((datasetStewardship) => datasetStewardship.save()) | ||
|
||
assertParamsHasDatasetId(params) | ||
assertParamsHasOnwerId(params) | ||
assertParamsHasSupportId(params) | ||
assertParamsHasDepartmentId(params) | ||
|
||
return DatasetStewardship.build({ | ||
id: sequence, | ||
datasetId: params.datasetId, // does not unbrand and cast datasetId to number | ||
ownerId: params.ownerId, | ||
supportId: params.supportId, | ||
departmentId: params.departmentId, | ||
}) | ||
} | ||
) | ||
|
||
export default datasetStewardshipFactory | ||
|
||
function assertParamsHasDatasetId( | ||
params: DeepPartial<DatasetStewardship> | ||
): asserts params is DeepPartial<DatasetStewardship> & { datasetId: number } { | ||
if (typeof params.datasetId !== "number") { | ||
throw new Error("datasetId is must be a number") | ||
} | ||
} | ||
|
||
function assertParamsHasOnwerId( | ||
params: DeepPartial<DatasetStewardship> | ||
): asserts params is DeepPartial<DatasetStewardship> & { ownerId: number } { | ||
if (typeof params.ownerId !== "number") { | ||
throw new Error("ownerId is must be a number") | ||
} | ||
} | ||
|
||
function assertParamsHasSupportId( | ||
params: DeepPartial<DatasetStewardship> | ||
): asserts params is DeepPartial<DatasetStewardship> & { supportId: number } { | ||
if (typeof params.supportId !== "number") { | ||
throw new Error("supportId is must be a number") | ||
} | ||
} | ||
|
||
function assertParamsHasDepartmentId( | ||
params: DeepPartial<DatasetStewardship> | ||
): asserts params is DeepPartial<DatasetStewardship> & { departmentId: number } { | ||
if (typeof params.departmentId !== "number") { | ||
throw new Error("departmentId is must be a number") | ||
} | ||
} |
This file contains 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
Oops, something went wrong.