Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import _ from 'lodash';
2import { type Opaque, type UnwrapOpaque } from 'type-fest';
3
4import { instantiateOpaqueType } from '../../utils/typescript-types.js';
5import {
6 type ItemType,
7 type ItemTypeIdentifier,
8} from '../moderationConfigService/index.js';
9import { type ItemSubmission } from './makeItemSubmission.js';
10
11const { omit } = _;
12
13export type ItemSubmissionWithTypeIdentifier<Type extends ItemType = ItemType> =
14 Opaque<
15 Omit<UnwrapOpaque<ItemSubmission<Type>>, 'itemType'> & {
16 itemTypeIdentifier: ItemTypeIdentifier;
17 },
18 'ItemSubmissionWithTypeIdentifier'
19 >;
20
21export function itemSubmissionToItemSubmissionWithTypeIdentifier(
22 it: ItemSubmission,
23) {
24 return instantiateOpaqueType<ItemSubmissionWithTypeIdentifier>({
25 ...omit(it, 'itemType'),
26 itemTypeIdentifier: {
27 id: it.itemType.id,
28 version: it.itemType.version,
29 schemaVariant: it.itemType.schemaVariant,
30 },
31 });
32}
33
34export function itemSubmissionWithTypeIdentifierToItemSubmission<
35 T extends ItemType = ItemType,
36>(it: ItemSubmissionWithTypeIdentifier, type: T) {
37 return instantiateOpaqueType<ItemSubmission<T>>({
38 submissionId: it.submissionId,
39 submissionTime: it.submissionTime,
40 itemId: it.itemId,
41 creator: it.creator,
42 data: it.data,
43 itemType: type,
44 });
45}