Skip to content

Commit

Permalink
fix: handling of set-aside cases in imported/restored documents (#1674)
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson authored Dec 5, 2024
1 parent d5189c9 commit f2b3e99
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
13 changes: 6 additions & 7 deletions v3/src/data-interactive/data-interactive-type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IGlobalValue } from "../models/global/global-value"
import { getSharedCaseMetadataFromDataset } from "../models/shared/shared-data-utils"
import { kAttrIdPrefix, maybeToV2Id, toV2Id, toV3AttrId } from "../utilities/codap-utils"
import {
ICodapV2AttributeV3, ICodapV2CollectionV3, ICodapV2DataContextV3, v3TypeFromV2TypeString
ICodapV2Attribute, ICodapV2CollectionV3, ICodapV2DataContextV3, v3TypeFromV2TypeString
} from "../v2/codap-v2-types"
import { DIAttribute, DIGetCaseResult, DIResources, DISingleValues } from "./data-interactive-types"
import { getCaseValues } from "./data-interactive-utils"
Expand Down Expand Up @@ -104,7 +104,7 @@ export function getCaseRequestResultValues(c: ICase, dataContext: IDataSet): DIG
}
}

export function convertAttributeToV2(attribute: IAttribute, dataContext?: IDataSet): ICodapV2AttributeV3 {
export function convertAttributeToV2(attribute: IAttribute, dataContext?: IDataSet): ICodapV2Attribute {
const metadata = dataContext && getSharedCaseMetadataFromDataset(dataContext)
const { cid, name, type, title, description, deleteable, editable, id, precision } = attribute
const v2Id = toV2Id(id)
Expand Down Expand Up @@ -143,11 +143,9 @@ export function convertCollectionToV2(collection: ICollectionModel, dataContext?
const { name, title, id, labels: _labels } = collection
const v2Id = toV2Id(id)
const labels = _labels ? getSnapshot(_labels) : undefined
const v2Attrs = collection.attributes.map(attribute => {
const attrs = collection.attributes.map(attribute => {
if (attribute) return convertAttributeToV2(attribute, dataContext)
})
const attrs: ICodapV2AttributeV3[] = []
v2Attrs.forEach(attr => attr && attrs.push(attr))
}).filter(attr => !!attr)
return {
// areParentChildLinksConfigured,
attrs,
Expand Down Expand Up @@ -184,7 +182,8 @@ export function convertDataSetToV2(dataSet: IDataSet, docId: number | string): I
description,
// metadata,
// preventReorg,
// setAsideItems,
// TODO_V2_EXPORT
setAsideItems: []
// contextStorage
}
}
Expand Down
4 changes: 2 additions & 2 deletions v3/src/data-interactive/data-interactive-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequireAtLeastOne } from "type-fest"
import { IAttribute } from "../models/data/attribute"
import { ICodapV2Attribute, ICodapV2AttributeV3, ICodapV2Collection, ICodapV2DataContext } from "../v2/codap-v2-types"
import { ICodapV2Attribute, ICodapV2Collection, ICodapV2DataContext } from "../v2/codap-v2-types"
import { IDataSet } from "../models/data/data-set"
import { ICase, ICaseID } from "../models/data/data-set-types"
import { IGlobalValue } from "../models/global/global-value"
Expand Down Expand Up @@ -192,7 +192,7 @@ export type DISingleValues = DIAttribute | DINotifyAttribute | DIAttributeLocati
export type DIValues = DISingleValues | DISingleValues[] | number | string[]

// types returned as outputs by the API
export type DIResultAttributes = { attrs: ICodapV2AttributeV3[] }
export type DIResultAttributes = { attrs: ICodapV2Attribute[] }
export type DIResultSingleValues = DICase | DIComponentInfo | DIGetCaseResult | DIGlobal | DIInteractiveFrame
export type DIResultValues = DIResultSingleValues | DIResultSingleValues[] |
DIAllCases | DIDeleteCollectionResult | DIUpdateItemResult | DIResultAttributes | number | number[]
Expand Down
2 changes: 1 addition & 1 deletion v3/src/models/data/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export const CollectionModel = V2Model
}
}

// item info is only stored for visible items
// item info is stored for all items
itemInfo.push({ itemId, caseId })
}
})
Expand Down
2 changes: 1 addition & 1 deletion v3/src/models/data/data-set-conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("DataSet conversions", () => {
expect(data.attributesMap.size).toBe(3)
expect(data.attributes.length).toBe(3)
expect(data._itemIds.length).toBe(1)
expect(data.itemIds.length).toBe(1)
expect(data.itemIds.length).toBe(0)
expect(data.setAsideItemIds.length).toBe(1)
})
})
3 changes: 3 additions & 0 deletions v3/src/models/data/data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,9 @@ export const DataSet = V2Model.named("DataSet").props({
self.addCollection({ name: t("DG.AppController.createDataSet.collectionName") })
}

// initialize setAsideItemIdsSet
self.setAsideItemIdsSet.replace(self.setAsideItemIds)

if (!srcDataSet) {
// set up middleware to add ids to inserted attributes and cases
// adding the ids in middleware makes them available as action arguments
Expand Down
18 changes: 16 additions & 2 deletions v3/src/v2/codap-v2-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SetRequired } from "type-fest"
import { IAttribute } from "../models/data/attribute"
import { ICollectionModel, ICollectionModelSnapshot } from "../models/data/collection"
import { IDataSet, toCanonical } from "../models/data/data-set"
import { ICaseCreation } from "../models/data/data-set-types"
import { ICaseCreation, IItem } from "../models/data/data-set-types"
import { v2NameTitleToV3Title } from "../models/data/v2-model"
import { IDocumentMetadata } from "../models/document/document-metadata"
import { ISharedCaseMetadata, SharedCaseMetadata } from "../models/shared/shared-case-metadata"
Expand All @@ -12,7 +12,7 @@ import {
} from "../utilities/codap-utils"
import {
CodapV2Component, ICodapV2Attribute, ICodapV2Case, ICodapV2Collection, ICodapV2DataContext, ICodapV2DocumentJson,
v3TypeFromV2TypeString
ICodapV2SetAsideItem, v3TypeFromV2TypeString
} from "./codap-v2-types"

interface V2CaseIdInfo {
Expand Down Expand Up @@ -112,6 +112,7 @@ export class CodapV2Document {
this.caseMetadataMap.set(guid, caseMetadata)

this.registerCollections(sharedDataSet.dataSet, caseMetadata, collections)
this.registerSetAsideItems(sharedDataSet.dataSet, context.setAsideItems)
})
}

Expand Down Expand Up @@ -223,4 +224,17 @@ export class CodapV2Document {
data.addCases(itemsToAdd)
}
}

registerSetAsideItems(data: IDataSet, setAsideItems?: ICodapV2SetAsideItem[]) {
const itemsToAdd: IItem[] = []
setAsideItems?.forEach(item => {
// some v2 documents don't store item ids, so we generate them if necessary
const { id = v3Id(kItemIdPrefix), values } = item
itemsToAdd.push({ __id__: id, ...toCanonical(data, values) })
})
if (itemsToAdd.length) {
data.addCases(itemsToAdd)
data.hideCasesOrItems(itemsToAdd.map(item => item.__id__))
}
}
}
22 changes: 10 additions & 12 deletions v3/src/v2/codap-v2-types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { SetOptional } from "type-fest"
import {AttributeType} from "../models/data/attribute"

export type AllowStringIds<T> = Omit<T, "guid" | "id"> & {
guid: number | string
id: number | string
}

export interface ICodapV2Attribute {
guid: number
id: number
Expand All @@ -31,8 +26,6 @@ export interface ICodapV2Attribute {
precision?: number | string | null
unit?: string | null
}
// when exporting a v3 attribute to v2
export type ICodapV2AttributeV3 = AllowStringIds<ICodapV2Attribute>

export function isCodapV2Attribute(o: any): o is ICodapV2Attribute {
return o.type === "DG.Attribute"
Expand Down Expand Up @@ -86,8 +79,13 @@ export interface ICodapV2Collection {
// when exporting a v3 collection to v2
type CollectionNotYetExported = "cases" | "caseName" | "childAttrName" | "collapseChildren"
export interface ICodapV2CollectionV3
extends SetOptional<Omit<AllowStringIds<ICodapV2Collection>, "attrs">, CollectionNotYetExported> {
attrs: ICodapV2AttributeV3[]
extends SetOptional<Omit<ICodapV2Collection, "attrs">, CollectionNotYetExported> {
attrs: ICodapV2Attribute[]
}

export interface ICodapV2SetAsideItem {
id: string // item id
values: Record<string, number | string>
}

export interface ICodapV2DataContextMetadata {
Expand All @@ -103,15 +101,15 @@ export interface ICodapV2DataContext {
title: string
collections: ICodapV2Collection[]
description?: string
metadata?: ICodapV2DataContextMetadata | null,
metadata?: ICodapV2DataContextMetadata | null
// preventReorg: boolean
// setAsideItems: this.get('dataSet').archiveSetAsideItems(),
setAsideItems?: ICodapV2SetAsideItem[]
// contextStorage: this.contextStorage
}
// when exporting a v3 data set to v2 data context
type DCNotYetExported = "flexibleGroupingChangeFlag"
export interface ICodapV2DataContextV3
extends SetOptional<Omit<AllowStringIds<ICodapV2DataContext>, "document" | "collections">, DCNotYetExported> {
extends SetOptional<Omit<ICodapV2DataContext, "document" | "collections">, DCNotYetExported> {
document: number | string
collections: ICodapV2CollectionV3[]
}
Expand Down

0 comments on commit f2b3e99

Please sign in to comment.