@geoprotocol/geo-sdk 0.19.2 → 0.19.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/examples/ranks/create-ordinal-rank.d.ts +1 -1
- package/dist/examples/ranks/create-ordinal-rank.js +9 -6
- package/dist/examples/ranks/create-ordinal-rank.js.map +1 -1
- package/dist/examples/ranks/create-weighted-rank.d.ts +1 -1
- package/dist/examples/ranks/create-weighted-rank.js +9 -6
- package/dist/examples/ranks/create-weighted-rank.js.map +1 -1
- package/dist/src/client/ranks.d.ts +36 -0
- package/dist/src/client/ranks.d.ts.map +1 -0
- package/dist/src/client/ranks.js +57 -0
- package/dist/src/client/ranks.js.map +1 -0
- package/dist/src/client/ranks.test.d.ts +2 -0
- package/dist/src/client/ranks.test.d.ts.map +1 -0
- package/dist/src/client/ranks.test.js +96 -0
- package/dist/src/client/ranks.test.js.map +1 -0
- package/dist/src/client.d.ts +5 -0
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/client.js +21 -0
- package/dist/src/client.js.map +1 -1
- package/dist/src/core/ids/system.d.ts +8 -0
- package/dist/src/core/ids/system.d.ts.map +1 -1
- package/dist/src/core/ids/system.js +8 -0
- package/dist/src/core/ids/system.js.map +1 -1
- package/dist/src/ops/index.d.ts +1 -0
- package/dist/src/ops/index.d.ts.map +1 -1
- package/dist/src/ops/index.js +1 -0
- package/dist/src/ops/index.js.map +1 -1
- package/dist/src/ops/ranks.d.ts +33 -0
- package/dist/src/ops/ranks.d.ts.map +1 -0
- package/dist/src/ops/ranks.js +33 -0
- package/dist/src/ops/ranks.js.map +1 -0
- package/dist/src/ops/ranks.test.d.ts +2 -0
- package/dist/src/ops/ranks.test.d.ts.map +1 -0
- package/dist/src/ops/ranks.test.js +36 -0
- package/dist/src/ops/ranks.test.js.map +1 -0
- package/dist/src/ranks/create-rank.d.ts +19 -9
- package/dist/src/ranks/create-rank.d.ts.map +1 -1
- package/dist/src/ranks/create-rank.js +34 -57
- package/dist/src/ranks/create-rank.js.map +1 -1
- package/dist/src/ranks/create-rank.test.js +151 -23
- package/dist/src/ranks/create-rank.test.js.map +1 -1
- package/dist/src/ranks/index.d.ts +0 -1
- package/dist/src/ranks/index.d.ts.map +1 -1
- package/dist/src/ranks/index.js +0 -1
- package/dist/src/ranks/index.js.map +1 -1
- package/dist/src/ranks/types.d.ts +30 -0
- package/dist/src/ranks/types.d.ts.map +1 -1
- package/dist/src/ranks/update-rank.d.ts +40 -0
- package/dist/src/ranks/update-rank.d.ts.map +1 -0
- package/dist/src/ranks/update-rank.js +61 -0
- package/dist/src/ranks/update-rank.js.map +1 -0
- package/dist/src/ranks/update-rank.test.d.ts +2 -0
- package/dist/src/ranks/update-rank.test.d.ts.map +1 -0
- package/dist/src/ranks/update-rank.test.js +120 -0
- package/dist/src/ranks/update-rank.test.js.map +1 -0
- package/dist/src/ranks/vote-ops.d.ts +37 -0
- package/dist/src/ranks/vote-ops.d.ts.map +1 -0
- package/dist/src/ranks/vote-ops.js +96 -0
- package/dist/src/ranks/vote-ops.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { CreateRankParams, CreateRankResult } from '../ranks/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Builds create-rank ops (pure, no network access).
|
|
4
|
+
*
|
|
5
|
+
* Each vote carries a `spaceId` (set as `to_space_id` on the vote relation), so
|
|
6
|
+
* a rank can include the same entity across multiple space perspectives; item
|
|
7
|
+
* uniqueness is keyed on `(entityId, spaceId)`. Pass an optional `blockId` to
|
|
8
|
+
* link the rank to a `Ranking Block`.
|
|
9
|
+
*
|
|
10
|
+
* For re-submissions use `geo.ranks.update(...)`, which fetches the rank's
|
|
11
|
+
* existing vote relations and supersedes them.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Ops } from '@geoprotocol/geo-sdk';
|
|
16
|
+
*
|
|
17
|
+
* const { id, ops, voteIds } = Ops.ranks.create({
|
|
18
|
+
* name: 'My Favorite Movies',
|
|
19
|
+
* rankType: 'ORDINAL',
|
|
20
|
+
* blockId, // optional
|
|
21
|
+
* votes: [
|
|
22
|
+
* { entityId: movie1Id, spaceId },
|
|
23
|
+
* { entityId: movie2Id, spaceId },
|
|
24
|
+
* ],
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param params Rank name, type, optional block link, and ordered votes.
|
|
29
|
+
* @returns Rank entity ID, ops, and the created vote entity IDs.
|
|
30
|
+
* @throws When any supplied ID is invalid, or a `(entityId, spaceId)` pair is duplicated.
|
|
31
|
+
*/
|
|
32
|
+
export declare const create: (params: CreateRankParams) => CreateRankResult;
|
|
33
|
+
//# sourceMappingURL=ranks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ranks.d.ts","sourceRoot":"","sources":["../../../src/ops/ranks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE5E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,eAAO,MAAM,MAAM,GAAI,QAAQ,gBAAgB,KAAG,gBAAsC,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createRank } from '../ranks/create-rank.js';
|
|
2
|
+
/**
|
|
3
|
+
* Builds create-rank ops (pure, no network access).
|
|
4
|
+
*
|
|
5
|
+
* Each vote carries a `spaceId` (set as `to_space_id` on the vote relation), so
|
|
6
|
+
* a rank can include the same entity across multiple space perspectives; item
|
|
7
|
+
* uniqueness is keyed on `(entityId, spaceId)`. Pass an optional `blockId` to
|
|
8
|
+
* link the rank to a `Ranking Block`.
|
|
9
|
+
*
|
|
10
|
+
* For re-submissions use `geo.ranks.update(...)`, which fetches the rank's
|
|
11
|
+
* existing vote relations and supersedes them.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Ops } from '@geoprotocol/geo-sdk';
|
|
16
|
+
*
|
|
17
|
+
* const { id, ops, voteIds } = Ops.ranks.create({
|
|
18
|
+
* name: 'My Favorite Movies',
|
|
19
|
+
* rankType: 'ORDINAL',
|
|
20
|
+
* blockId, // optional
|
|
21
|
+
* votes: [
|
|
22
|
+
* { entityId: movie1Id, spaceId },
|
|
23
|
+
* { entityId: movie2Id, spaceId },
|
|
24
|
+
* ],
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param params Rank name, type, optional block link, and ordered votes.
|
|
29
|
+
* @returns Rank entity ID, ops, and the created vote entity IDs.
|
|
30
|
+
* @throws When any supplied ID is invalid, or a `(entityId, spaceId)` pair is duplicated.
|
|
31
|
+
*/
|
|
32
|
+
export const create = (params) => createRank(params);
|
|
33
|
+
//# sourceMappingURL=ranks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ranks.js","sourceRoot":"","sources":["../../../src/ops/ranks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,MAAwB,EAAoB,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ranks.test.d.ts","sourceRoot":"","sources":["../../../src/ops/ranks.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RANK_BLOCK_RELATION_TYPE, RANK_VOTES_RELATION_TYPE } from '../core/ids/system.js';
|
|
3
|
+
import { toGrcId } from '../id-utils.js';
|
|
4
|
+
import { create } from './ranks.js';
|
|
5
|
+
const MOVIE_1 = 'f47ac10b58cc4372a5670e02b2c3d479';
|
|
6
|
+
const MOVIE_2 = '550e8400e29b41d4a716446655440000';
|
|
7
|
+
const SPACE_ID = 'd4bc2f205e2d415e971eb0b9fbf6b6fc';
|
|
8
|
+
const BLOCK_ID = '9f8e7d6c5b4a49388271605f4e3d2c1b';
|
|
9
|
+
function relationsOfType(ops, typeId) {
|
|
10
|
+
return ops.filter((op) => op.type === 'createRelation' &&
|
|
11
|
+
op.relationType.every((byte, index) => byte === toGrcId(typeId)[index]));
|
|
12
|
+
}
|
|
13
|
+
describe('Ops.ranks.create', () => {
|
|
14
|
+
it('builds rank creation ops (pure, no network)', () => {
|
|
15
|
+
const result = create({
|
|
16
|
+
name: 'Top movies',
|
|
17
|
+
rankType: 'ORDINAL',
|
|
18
|
+
votes: [
|
|
19
|
+
{ entityId: MOVIE_1, spaceId: SPACE_ID },
|
|
20
|
+
{ entityId: MOVIE_2, spaceId: SPACE_ID },
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
expect(result.voteIds).toHaveLength(2);
|
|
24
|
+
expect(relationsOfType(result.ops, RANK_VOTES_RELATION_TYPE)).toHaveLength(2);
|
|
25
|
+
});
|
|
26
|
+
it('links the rank to a Ranking Block when blockId is provided', () => {
|
|
27
|
+
const result = create({
|
|
28
|
+
name: 'Block-linked rank',
|
|
29
|
+
rankType: 'ORDINAL',
|
|
30
|
+
blockId: BLOCK_ID,
|
|
31
|
+
votes: [{ entityId: MOVIE_1, spaceId: SPACE_ID }],
|
|
32
|
+
});
|
|
33
|
+
expect(relationsOfType(result.ops, RANK_BLOCK_RELATION_TYPE)).toHaveLength(1);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
//# sourceMappingURL=ranks.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ranks.test.js","sourceRoot":"","sources":["../../../src/ops/ranks.test.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CA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,GAAG,EAAE;QACpE,MAAM,MAAM,GAAG,MAAM,CAAC;YACpB,IAAI,EAAE,mBAAmB;YACzB,QAAQ,EAAE,SAAS;YACnB,OAAO,EAAE,QAAQ;YACjB,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC;SAClD,CAAC,CAAC;QAEH,MAAM,CAAC,eAAe,CAAC,MAAM,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IAChF,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -4,8 +4,17 @@ import type { CreateRankParams, CreateRankResult } from './types.js';
|
|
|
4
4
|
* All IDs passed to this function are validated. If any invalid ID is provided,
|
|
5
5
|
* the function will throw an error.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Each vote must carry a `spaceId` that scopes the ranked entity to a space
|
|
8
|
+
* perspective (set as `to_space_id` on the vote relation). A rank may therefore
|
|
9
|
+
* include the same entity across multiple spaces; item uniqueness is keyed on
|
|
10
|
+
* `(entityId, spaceId)`.
|
|
11
|
+
*
|
|
12
|
+
* A fractional index is generated from the array order and set as each vote
|
|
13
|
+
* relation's `position`, so clients can order votes natively. For ORDINAL ranks
|
|
14
|
+
* the same fractional index is stored on the reified vote entity.
|
|
15
|
+
*
|
|
16
|
+
* When `blockId` is provided, a `Rank → Ranking Block` relation is emitted to
|
|
17
|
+
* associate the rank with a ranking block. The link may also be added later.
|
|
9
18
|
*
|
|
10
19
|
* @example
|
|
11
20
|
* ```ts
|
|
@@ -15,10 +24,11 @@ import type { CreateRankParams, CreateRankResult } from './types.js';
|
|
|
15
24
|
* name: 'My Favorite Movies',
|
|
16
25
|
* description: 'A ranked list of my favorite movies', // optional
|
|
17
26
|
* rankType: 'ORDINAL',
|
|
27
|
+
* blockId, // optional, links the rank to a Ranking Block
|
|
18
28
|
* votes: [
|
|
19
|
-
* { entityId: movie1Id }, // 1st place
|
|
20
|
-
* { entityId: movie2Id }, // 2nd place
|
|
21
|
-
* { entityId: movie3Id }, // 3rd place
|
|
29
|
+
* { entityId: movie1Id, spaceId }, // 1st place
|
|
30
|
+
* { entityId: movie2Id, spaceId }, // 2nd place
|
|
31
|
+
* { entityId: movie3Id, spaceId }, // 3rd place
|
|
22
32
|
* ],
|
|
23
33
|
* });
|
|
24
34
|
*
|
|
@@ -27,8 +37,8 @@ import type { CreateRankParams, CreateRankResult } from './types.js';
|
|
|
27
37
|
* name: 'Restaurant Ratings',
|
|
28
38
|
* rankType: 'WEIGHTED',
|
|
29
39
|
* votes: [
|
|
30
|
-
* { entityId: restaurant1Id, value: 4.5 }, // numeric score
|
|
31
|
-
* { entityId: restaurant2Id, value: 3.8 },
|
|
40
|
+
* { entityId: restaurant1Id, spaceId, value: 4.5 }, // numeric score
|
|
41
|
+
* { entityId: restaurant2Id, spaceId, value: 3.8 },
|
|
32
42
|
* ],
|
|
33
43
|
* });
|
|
34
44
|
* ```
|
|
@@ -36,7 +46,7 @@ import type { CreateRankParams, CreateRankResult } from './types.js';
|
|
|
36
46
|
* @param params – {@link CreateRankParams}
|
|
37
47
|
* @returns – {@link CreateRankResult}
|
|
38
48
|
* @throws Will throw an error if any provided ID is invalid
|
|
39
|
-
* @throws Will throw an error if any entityId is duplicated in votes
|
|
49
|
+
* @throws Will throw an error if any `(entityId, spaceId)` pair is duplicated in votes
|
|
40
50
|
*/
|
|
41
|
-
export declare const createRank: ({ id: providedId, name, description, rankType, votes, }: CreateRankParams) => CreateRankResult;
|
|
51
|
+
export declare const createRank: ({ id: providedId, name, description, rankType, blockId, votes, }: CreateRankParams) => CreateRankResult;
|
|
42
52
|
//# sourceMappingURL=create-rank.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-rank.d.ts","sourceRoot":"","sources":["../../../src/ranks/create-rank.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-rank.d.ts","sourceRoot":"","sources":["../../../src/ranks/create-rank.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAGrE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,eAAO,MAAM,UAAU,GAAI,kEAOxB,gBAAgB,KAAG,gBAiFrB,CAAC"}
|
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import { createEntity as grcCreateEntity, createRelation as grcCreateRelation, languages, } from '@geoprotocol/grc-20';
|
|
2
|
-
import {
|
|
3
|
-
import { DESCRIPTION_PROPERTY, NAME_PROPERTY, RANK_TYPE, RANK_TYPE_PROPERTY, RANK_VOTES_RELATION_TYPE, TYPES_PROPERTY, VOTE_ORDINAL_VALUE_PROPERTY, VOTE_WEIGHTED_VALUE_PROPERTY, } from '../core/ids/system.js';
|
|
2
|
+
import { DESCRIPTION_PROPERTY, NAME_PROPERTY, RANK_BLOCK_RELATION_TYPE, RANK_TYPE, RANK_TYPE_PROPERTY, TYPES_PROPERTY, } from '../core/ids/system.js';
|
|
4
3
|
import { Id } from '../id.js';
|
|
5
4
|
import { assertValid, generate, toGrcId } from '../id-utils.js';
|
|
5
|
+
import { buildVoteOps, validateVotes } from './vote-ops.js';
|
|
6
6
|
/**
|
|
7
7
|
* Creates a rank entity with the given name, description, rankType, and votes.
|
|
8
8
|
* All IDs passed to this function are validated. If any invalid ID is provided,
|
|
9
9
|
* the function will throw an error.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
11
|
+
* Each vote must carry a `spaceId` that scopes the ranked entity to a space
|
|
12
|
+
* perspective (set as `to_space_id` on the vote relation). A rank may therefore
|
|
13
|
+
* include the same entity across multiple spaces; item uniqueness is keyed on
|
|
14
|
+
* `(entityId, spaceId)`.
|
|
15
|
+
*
|
|
16
|
+
* A fractional index is generated from the array order and set as each vote
|
|
17
|
+
* relation's `position`, so clients can order votes natively. For ORDINAL ranks
|
|
18
|
+
* the same fractional index is stored on the reified vote entity.
|
|
19
|
+
*
|
|
20
|
+
* When `blockId` is provided, a `Rank → Ranking Block` relation is emitted to
|
|
21
|
+
* associate the rank with a ranking block. The link may also be added later.
|
|
13
22
|
*
|
|
14
23
|
* @example
|
|
15
24
|
* ```ts
|
|
@@ -19,10 +28,11 @@ import { assertValid, generate, toGrcId } from '../id-utils.js';
|
|
|
19
28
|
* name: 'My Favorite Movies',
|
|
20
29
|
* description: 'A ranked list of my favorite movies', // optional
|
|
21
30
|
* rankType: 'ORDINAL',
|
|
31
|
+
* blockId, // optional, links the rank to a Ranking Block
|
|
22
32
|
* votes: [
|
|
23
|
-
* { entityId: movie1Id }, // 1st place
|
|
24
|
-
* { entityId: movie2Id }, // 2nd place
|
|
25
|
-
* { entityId: movie3Id }, // 3rd place
|
|
33
|
+
* { entityId: movie1Id, spaceId }, // 1st place
|
|
34
|
+
* { entityId: movie2Id, spaceId }, // 2nd place
|
|
35
|
+
* { entityId: movie3Id, spaceId }, // 3rd place
|
|
26
36
|
* ],
|
|
27
37
|
* });
|
|
28
38
|
*
|
|
@@ -31,8 +41,8 @@ import { assertValid, generate, toGrcId } from '../id-utils.js';
|
|
|
31
41
|
* name: 'Restaurant Ratings',
|
|
32
42
|
* rankType: 'WEIGHTED',
|
|
33
43
|
* votes: [
|
|
34
|
-
* { entityId: restaurant1Id, value: 4.5 }, // numeric score
|
|
35
|
-
* { entityId: restaurant2Id, value: 3.8 },
|
|
44
|
+
* { entityId: restaurant1Id, spaceId, value: 4.5 }, // numeric score
|
|
45
|
+
* { entityId: restaurant2Id, spaceId, value: 3.8 },
|
|
36
46
|
* ],
|
|
37
47
|
* });
|
|
38
48
|
* ```
|
|
@@ -40,28 +50,19 @@ import { assertValid, generate, toGrcId } from '../id-utils.js';
|
|
|
40
50
|
* @param params – {@link CreateRankParams}
|
|
41
51
|
* @returns – {@link CreateRankResult}
|
|
42
52
|
* @throws Will throw an error if any provided ID is invalid
|
|
43
|
-
* @throws Will throw an error if any entityId is duplicated in votes
|
|
53
|
+
* @throws Will throw an error if any `(entityId, spaceId)` pair is duplicated in votes
|
|
44
54
|
*/
|
|
45
|
-
export const createRank = ({ id: providedId, name, description, rankType, votes, }) => {
|
|
55
|
+
export const createRank = ({ id: providedId, name, description, rankType, blockId, votes, }) => {
|
|
46
56
|
// Validate all input IDs
|
|
47
57
|
if (providedId) {
|
|
48
58
|
assertValid(providedId, '`id` in `createRank`');
|
|
49
59
|
}
|
|
50
|
-
|
|
51
|
-
assertValid(
|
|
52
|
-
}
|
|
53
|
-
// Validate no duplicate entity IDs in votes
|
|
54
|
-
const seenEntityIds = new Set();
|
|
55
|
-
for (const vote of votes) {
|
|
56
|
-
const entityId = String(vote.entityId);
|
|
57
|
-
if (seenEntityIds.has(entityId)) {
|
|
58
|
-
throw new Error(`Duplicate entityId in votes: "${entityId}". Each entity can only be voted once per rank.`);
|
|
59
|
-
}
|
|
60
|
-
seenEntityIds.add(entityId);
|
|
60
|
+
if (blockId) {
|
|
61
|
+
assertValid(blockId, '`blockId` in `createRank`');
|
|
61
62
|
}
|
|
63
|
+
validateVotes(votes, rankType, 'createRank');
|
|
62
64
|
const id = providedId ?? generate();
|
|
63
65
|
const ops = [];
|
|
64
|
-
const voteIds = [];
|
|
65
66
|
// Create rank entity values
|
|
66
67
|
const rankValues = [
|
|
67
68
|
{
|
|
@@ -104,43 +105,19 @@ export const createRank = ({ id: providedId, name, description, rankType, votes,
|
|
|
104
105
|
to: toGrcId(RANK_TYPE),
|
|
105
106
|
relationType: toGrcId(TYPES_PROPERTY),
|
|
106
107
|
}));
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
// Create votes
|
|
110
|
-
votes.forEach((vote, i) => {
|
|
111
|
-
const voteEntityId = generate();
|
|
112
|
-
const relationId = generate();
|
|
113
|
-
voteIds.push(voteEntityId);
|
|
114
|
-
// Create relation from rank to voted entity
|
|
108
|
+
// Optionally link the rank to its Ranking Block
|
|
109
|
+
if (blockId) {
|
|
115
110
|
ops.push(grcCreateRelation({
|
|
116
|
-
id: toGrcId(
|
|
117
|
-
entity: toGrcId(
|
|
111
|
+
id: toGrcId(generate()),
|
|
112
|
+
entity: toGrcId(generate()),
|
|
118
113
|
from: toGrcId(id),
|
|
119
|
-
to: toGrcId(
|
|
120
|
-
relationType: toGrcId(
|
|
114
|
+
to: toGrcId(blockId),
|
|
115
|
+
relationType: toGrcId(RANK_BLOCK_RELATION_TYPE),
|
|
121
116
|
}));
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
value: {
|
|
127
|
-
type: 'text',
|
|
128
|
-
value: fractionalIndices[i],
|
|
129
|
-
language: languages.english(),
|
|
130
|
-
},
|
|
131
|
-
}
|
|
132
|
-
: {
|
|
133
|
-
property: toGrcId(VOTE_WEIGHTED_VALUE_PROPERTY),
|
|
134
|
-
value: {
|
|
135
|
-
type: 'float',
|
|
136
|
-
value: vote.value,
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
ops.push(grcCreateEntity({
|
|
140
|
-
id: toGrcId(voteEntityId),
|
|
141
|
-
values: [voteValue],
|
|
142
|
-
}));
|
|
143
|
-
});
|
|
117
|
+
}
|
|
118
|
+
// Create vote relations + reified vote entities
|
|
119
|
+
const { ops: voteOps, voteIds } = buildVoteOps(id, rankType, votes);
|
|
120
|
+
ops.push(...voteOps);
|
|
144
121
|
return { id: Id(id), ops, voteIds };
|
|
145
122
|
};
|
|
146
123
|
//# sourceMappingURL=create-rank.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-rank.js","sourceRoot":"","sources":["../../../src/ranks/create-rank.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,eAAe,EAC/B,cAAc,IAAI,iBAAiB,EACnC,SAAS,GAEV,MAAM,qBAAqB,CAAC;AAC7B,OAAO,
|
|
1
|
+
{"version":3,"file":"create-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