-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
187637447 v3 DI caseByID and caseByIndex Requests (#1276)
* Handle delete, get, and update caseByID requests. * Handle delete, get, and update caseByIndex requests.
- Loading branch information
1 parent
e46fc37
commit ffac044
Showing
22 changed files
with
379 additions
and
97 deletions.
There are no files selected for viewing
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
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
50 changes: 50 additions & 0 deletions
50
v3/src/data-interactive/handlers/case-by-handler-functions.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,50 @@ | ||
import { ICase } from "../../models/data/data-set-types" | ||
import { t } from "../../utilities/translation/translate" | ||
import { DIResources, DIUpdateCase, DIValues } from "../data-interactive-types" | ||
import { getCaseRequestResultValues } from "../data-interactive-type-utils" | ||
import { attrNamesToIds } from "../data-interactive-utils" | ||
import { caseNotFoundResult, dataContextNotFoundResult } from "./di-results" | ||
|
||
export function deleteCaseBy(resources: DIResources, aCase?: ICase) { | ||
const { dataContext } = resources | ||
if (!dataContext) return dataContextNotFoundResult | ||
if (!aCase) return caseNotFoundResult | ||
|
||
const pseudoCase = dataContext.pseudoCaseMap.get(aCase.__id__) | ||
const cases = pseudoCase?.childCaseIds ?? [aCase.__id__] | ||
|
||
dataContext.applyModelChange(() => { | ||
dataContext.removeCases(cases) | ||
}) | ||
|
||
return { success: true } | ||
} | ||
|
||
export function getCaseBy(resources: DIResources, aCase?: ICase) { | ||
const { dataContext } = resources | ||
if (!dataContext) return dataContextNotFoundResult | ||
if (!aCase) return caseNotFoundResult | ||
|
||
return { success: true, values: getCaseRequestResultValues(aCase, dataContext) } as const | ||
} | ||
|
||
export function updateCaseBy(resources: DIResources, values?: DIValues, aCase?: ICase) { | ||
const { dataContext } = resources | ||
if (!dataContext) return dataContextNotFoundResult | ||
if (!aCase) return caseNotFoundResult | ||
|
||
const missingFieldResult = { | ||
success: false, | ||
values: { error: t("V3.DI.Error.fieldRequired", { vars: ["update", "caseByID/Index", "values.values"] }) } | ||
} as const | ||
if (!values) return missingFieldResult | ||
const updateCase = values as DIUpdateCase | ||
if (!updateCase.values) return missingFieldResult | ||
|
||
dataContext.applyModelChange(() => { | ||
const updatedAttributes = attrNamesToIds(updateCase.values, dataContext) | ||
dataContext.setCaseValues([{ ...updatedAttributes, __id__: aCase.__id__ }]) | ||
}) | ||
|
||
return { success: true } | ||
} |
66 changes: 66 additions & 0 deletions
66
v3/src/data-interactive/handlers/case-by-id-handler.test.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,66 @@ | ||
import { maybeToV2Id } from "../../utilities/codap-utils" | ||
import { DIGetCaseResult } from "../data-interactive-types" | ||
import { diCaseByIDHandler } from "./case-by-id-handler" | ||
import { setupTestDataset } from "./handler-test-utils" | ||
|
||
describe("DataInteractive CaseByIDHandler", () => { | ||
const handler = diCaseByIDHandler | ||
function setup() { | ||
const { dataset, a3 } = setupTestDataset() | ||
// eslint-disable-next-line no-unused-expressions | ||
dataset.collectionGroups | ||
const aCase = dataset.getCaseAtIndex(4) | ||
const caseId = aCase!.__id__ | ||
const pseudoCase = Array.from(dataset.pseudoCaseMap.values())[1].pseudoCase | ||
const pseudoCaseId = pseudoCase.__id__ | ||
return { dataContext: dataset, aCase, caseId, pseudoCase, pseudoCaseId, a3 } | ||
} | ||
|
||
it("get works as expected", () => { | ||
const { dataContext, aCase, caseId, pseudoCase, pseudoCaseId } = setup() | ||
|
||
expect(handler.get?.({})?.success).toBe(false) | ||
expect(handler.get?.({ dataContext })?.success).toBe(false) | ||
expect(handler.get?.({ caseByID: aCase })?.success).toBe(false) | ||
|
||
const caseResult = handler.get?.({ dataContext, caseByID: aCase })?.values as DIGetCaseResult | ||
expect(caseResult.case.id).toBe(maybeToV2Id(caseId)) | ||
|
||
const pseudoCaseResult = handler.get?.({ dataContext, caseByID: pseudoCase })?.values as DIGetCaseResult | ||
expect(pseudoCaseResult.case.id).toBe(maybeToV2Id(pseudoCaseId)) | ||
}) | ||
|
||
it("update works as expected", () => { | ||
const { dataContext, aCase, caseId, pseudoCase, pseudoCaseId, a3 } = setup() | ||
const caseResources = { dataContext, caseByID: aCase } | ||
|
||
expect(handler.update?.({}).success).toBe(false) | ||
expect(handler.update?.({ dataContext }).success).toBe(false) | ||
expect(handler.update?.(caseResources).success).toBe(false) | ||
expect(handler.update?.(caseResources, {}).success).toBe(false) | ||
|
||
expect(handler.update?.(caseResources, { values: { a3: 10 } }).success).toBe(true) | ||
expect(a3.numValues[dataContext.caseIndexFromID(caseId)!]).toBe(10) | ||
|
||
expect(handler.update?.({ dataContext, caseByID: pseudoCase }, { values: { a3: 100 } }).success).toBe(true) | ||
dataContext.pseudoCaseMap.get(pseudoCaseId)?.childCaseIds.forEach(id => { | ||
expect(a3.numValues[dataContext.caseIndexFromID(id)!]).toBe(100) | ||
}) | ||
}) | ||
|
||
it("delete works as expected", () => { | ||
const { dataContext, aCase, caseId, pseudoCase, pseudoCaseId } = setup() | ||
|
||
expect(handler.delete?.({}).success).toBe(false) | ||
expect(handler.delete?.({ dataContext }).success).toBe(false) | ||
|
||
expect(dataContext.getCase(caseId)).toBeDefined() | ||
expect(handler.delete?.({ dataContext, caseByID: aCase }).success).toBe(true) | ||
expect(dataContext.getCase(caseId)).toBeUndefined() | ||
|
||
const childCaseIds = dataContext.pseudoCaseMap.get(pseudoCaseId)!.childCaseIds | ||
childCaseIds.forEach(id => expect(dataContext.getCase(id)).toBeDefined()) | ||
expect(handler.delete?.({ dataContext, caseByID: pseudoCase }).success).toBe(true) | ||
childCaseIds.forEach(id => expect(dataContext.getCase(id)).toBeUndefined()) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import { registerDIHandler } from "../data-interactive-handler" | ||
import { DIHandler, diNotImplementedYet } from "../data-interactive-types" | ||
import { DIHandler, DIResources, DIValues } from "../data-interactive-types" | ||
import { deleteCaseBy, getCaseBy, updateCaseBy } from "./case-by-handler-functions" | ||
|
||
export const diCaseByIDHandler: DIHandler = { | ||
delete: diNotImplementedYet, | ||
get: diNotImplementedYet, | ||
update: diNotImplementedYet | ||
delete(resources: DIResources) { | ||
return deleteCaseBy(resources, resources.caseByID) | ||
}, | ||
get(resources: DIResources) { | ||
return getCaseBy(resources, resources.caseByID) | ||
}, | ||
update(resources: DIResources, values?: DIValues) { | ||
return updateCaseBy(resources, values, resources.caseByID) | ||
} | ||
} | ||
|
||
registerDIHandler("caseByID", diCaseByIDHandler) |
Oops, something went wrong.