generated from helsingborg-stad/gdi-template
-
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.
- Loading branch information
Showing
6 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
14 changes: 7 additions & 7 deletions
14
src/snapshot/adverts-snapshot.ts → src/snapshot/adverts/adverts-snapshot.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { PassThrough } from 'stream' | ||
import type { ImportSnapshotFunction, SnapshotFunction } from '../types' | ||
import { jsonStream, objectStream } from '../../lib/streams' | ||
import type { Category } from '../../categories/types' | ||
import { patchCategories } from './patch-categories' | ||
|
||
export const categoriesSnapshot: SnapshotFunction = (ctx, { categories }) => { | ||
const stream = new PassThrough() | ||
ctx.type = 'application/json' | ||
ctx.body = stream | ||
|
||
objectStream<Category, Category>( | ||
() => categories.getCategories(), | ||
async c => c | ||
) | ||
.pipe( | ||
jsonStream({ | ||
prefix: '{"snapshot": "categories", "categories": [', | ||
separator: ',', | ||
terminator: ']}', | ||
}) | ||
) | ||
.pipe(stream) | ||
} | ||
|
||
export const importCategoriesSnapshot: ImportSnapshotFunction = async ( | ||
user, | ||
{ categories }, | ||
data | ||
) => { | ||
const patched = patchCategories(await categories.getCategories(), data) | ||
return categories.updateCategories(patched) | ||
} |
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,40 @@ | ||
import type { Category } from '../../categories/types' | ||
import { patchCategories } from './patch-categories' | ||
|
||
const cat = (c: Partial<Category>): Category => ({ | ||
id: '', | ||
parentId: '', | ||
label: '', | ||
co2kg: 0, | ||
valueByUnit: 0, | ||
...c, | ||
}) | ||
describe('patchCategories', () => { | ||
it('does not update existing', () => { | ||
const initial = [cat({ id: 'c1', label: 'C1' })] | ||
const patch = [cat({ id: 'c1', label: 'modified C1' })] | ||
|
||
expect(patchCategories(initial, patch)).toMatchObject(initial) | ||
}) | ||
|
||
it('ignores unlabelled', () => { | ||
const initial = [cat({ id: 'c1', label: 'C1' })] | ||
const patch = [cat({ id: 'c1', label: '' }), cat({ id: 'c2' })] | ||
|
||
expect(patchCategories(initial, patch)).toMatchObject(initial) | ||
}) | ||
|
||
it('fixes bad parents', () => { | ||
const initial = [cat({ id: 'c1', label: 'C1' })] | ||
const patch = [ | ||
cat({ id: 'c2', label: 'C2', parentId: 'missing' }), | ||
cat({ id: 'c3', parentId: 'c2', label: 'C3' }), | ||
] | ||
|
||
expect(patchCategories(initial, patch)).toMatchObject([ | ||
cat({ id: 'c1', label: 'C1' }), | ||
cat({ id: 'c2', label: 'C2', parentId: '' }), | ||
cat({ id: 'c3', parentId: 'c2', label: 'C3' }), | ||
]) | ||
}) | ||
}) |
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,21 @@ | ||
import { normalizeCategories } from '../../categories/category-adapter' | ||
import type { Category } from '../../categories/types' | ||
import { uniqueBy } from '../../lib' | ||
|
||
export const patchCategories = ( | ||
existing: Category[], | ||
patch: Category[] | ||
): Category[] => | ||
fixParentLinks( | ||
[...normalizeCategories(existing), ...normalizeCategories(patch)] | ||
.filter(uniqueBy(({ id }) => id)) | ||
.filter(({ label }) => label) | ||
) | ||
|
||
const fixParentLinks = (cats: Category[]): Category[] => { | ||
const ids = new Set<String>(cats.map(({ id }) => id)) | ||
return cats.map(c => ({ | ||
...c, | ||
parentId: ids.has(c.parentId) ? c.parentId : '', | ||
})) | ||
} |
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