Skip to content

Commit

Permalink
Split points classes (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam authored Apr 16, 2024
1 parent db643ec commit 59afa95
Show file tree
Hide file tree
Showing 37 changed files with 2,303 additions and 1,410 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- name: Install pnpm
id: pnpm-install
uses: pnpm/action-setup@v2
uses: pnpm/action-setup@v3
with:
version: 8
run_install: false
Expand All @@ -34,7 +34,7 @@ jobs:
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
Expand Down
2 changes: 1 addition & 1 deletion composites/points/composite.json

Large diffs are not rendered by default.

40 changes: 13 additions & 27 deletions composites/points/schemas/1-init.graphql
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
#
# Generic interface used by all point allocations
# Generic interface used by all point allocations and aggregations
#

interface GenericPointAllocation
@createModel(description: "Interface for a generic point allocation to an account") {
issuer: DID! @documentAccount
recipient: DID! @accountReference
}

#
# Interface specific to a single point allocation
#

interface SinglePointAllocation implements GenericPointAllocation
@createModel(description: "Interface for the allocation of a single point to an account") {
interface PointsInterface
@createModel(description: "Interface for a generic points association to an account") {
issuer: DID! @documentAccount
recipient: DID! @accountReference
points: Int!
}

#
# Interfaces for multiple points allocation and aggregations
# Interfaces for points allocations and aggregations
#

interface MultiplePointsAllocation implements GenericPointAllocation
@createModel(description: "Interface for the allocation of multiple points to an account") {
interface PointsAllocationInterface implements PointsInterface
@createModel(description: "Interface for a single allocation of points to an account") {
issuer: DID! @documentAccount
recipient: DID! @accountReference
points: Int!
}

interface PointsAggregation @createModel(description: "Interface for points aggregation") {
interface PointsAggregationInterface implements PointsInterface
@createModel(description: "Interface for an aggregation of points to an account") {
issuer: DID! @documentAccount
recipient: DID! @accountReference
points: Int!
Expand All @@ -40,22 +32,16 @@ interface PointsAggregation @createModel(description: "Interface for points aggr
# Default models implementing the interfaces
#

type SinglePoint implements SinglePointAllocation
@createModel(description: "Allocation of a single point to an account") {
issuer: DID! @documentAccount
recipient: DID! @accountReference
}

type MultiplePoints implements MultiplePointsAllocation
@createModel(description: "Allocation of multiple points to an account") {
type SimplePointsAllocation implements PointsAllocationInterface
@createModel(description: "Simple points allocation to an account") {
issuer: DID! @documentAccount
recipient: DID! @accountReference
points: Int!
}

type TotalPoints implements PointsAggregation
type SimplePointsAggregation implements PointsAggregationInterface
@createModel(
description: "Points aggregation at a specific date"
description: "Simple points aggregation to an account at a specific date"
accountRelation: SET
accountRelationFields: ["recipient"]
) {
Expand Down
119 changes: 50 additions & 69 deletions composites/points/src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,109 @@
import type { RuntimeCompositeDefinition } from '@composedb/types'
export const definition: RuntimeCompositeDefinition = {
models: {
GenericPointAllocation: {
PointsAggregationInterface: {
interface: true,
implements: [],
id: 'kjzl6hvfrbw6c6m3n64vb2h4n8nxq9jjfb7sf7a9y893spm1pjd0enrsdlyphg4',
implements: ['kjzl6hvfrbw6c6lxvcf8bc07wjyn29ocoxqn877uia1y86qph79axtdrcuijpeo'],
id: 'kjzl6hvfrbw6c5m5bxe6jl7cocyxpg9b8em5w9mo3l8ws4zl5c0tu5vgapitpvk',
accountRelation: { type: 'none' },
},
MultiplePoints: {
interface: false,
implements: [
'kjzl6hvfrbw6ca7buvthejhv7vqr85vmpsepzj0mc6665y2zarvjbuvtm3v4kah',
'kjzl6hvfrbw6c6m3n64vb2h4n8nxq9jjfb7sf7a9y893spm1pjd0enrsdlyphg4',
],
id: 'kjzl6hvfrbw6c9fmjjdsbuxnewf0yhvco3dn5mihiogeso6i1csdbw2fq8oeijy',
accountRelation: { type: 'list' },
},
MultiplePointsAllocation: {
PointsAllocationInterface: {
interface: true,
implements: ['kjzl6hvfrbw6c6m3n64vb2h4n8nxq9jjfb7sf7a9y893spm1pjd0enrsdlyphg4'],
id: 'kjzl6hvfrbw6ca7buvthejhv7vqr85vmpsepzj0mc6665y2zarvjbuvtm3v4kah',
implements: ['kjzl6hvfrbw6c6lxvcf8bc07wjyn29ocoxqn877uia1y86qph79axtdrcuijpeo'],
id: 'kjzl6hvfrbw6cakj74rf7d3qjnm3xoydcgx7orzw4bwdmc6kljd04uojuhpef2j',
accountRelation: { type: 'none' },
},
PointsAggregation: {
PointsInterface: {
interface: true,
implements: [],
id: 'kjzl6hvfrbw6cb6393dpd8blke5w8r7pvbl4449mxetuibcav3oab8fnxmys6d6',
id: 'kjzl6hvfrbw6c6lxvcf8bc07wjyn29ocoxqn877uia1y86qph79axtdrcuijpeo',
accountRelation: { type: 'none' },
},
SinglePoint: {
SimplePointsAggregation: {
interface: false,
implements: [
'kjzl6hvfrbw6c7ilzfpjw96drd04jadb0aybiklk70ys2imxp5mjbjmgkecgddf',
'kjzl6hvfrbw6c6m3n64vb2h4n8nxq9jjfb7sf7a9y893spm1pjd0enrsdlyphg4',
'kjzl6hvfrbw6c5m5bxe6jl7cocyxpg9b8em5w9mo3l8ws4zl5c0tu5vgapitpvk',
'kjzl6hvfrbw6c6lxvcf8bc07wjyn29ocoxqn877uia1y86qph79axtdrcuijpeo',
],
id: 'kjzl6hvfrbw6c9332q9di7qfshxczet94w2tzeubvkbkk9vtuwmya6s9f1bvx9p',
accountRelation: { type: 'list' },
},
SinglePointAllocation: {
interface: true,
implements: ['kjzl6hvfrbw6c6m3n64vb2h4n8nxq9jjfb7sf7a9y893spm1pjd0enrsdlyphg4'],
id: 'kjzl6hvfrbw6c7ilzfpjw96drd04jadb0aybiklk70ys2imxp5mjbjmgkecgddf',
accountRelation: { type: 'none' },
id: 'kjzl6hvfrbw6capj3or1esf65c1jbluhky3t2wupxefeuocqt5lz5u4gum07o24',
accountRelation: { type: 'set', fields: ['recipient'] },
},
TotalPoints: {
SimplePointsAllocation: {
interface: false,
implements: ['kjzl6hvfrbw6cb6393dpd8blke5w8r7pvbl4449mxetuibcav3oab8fnxmys6d6'],
id: 'kjzl6hvfrbw6c5be4exsm7nkkksnqatki8bceyvbgtmcu2bu5vign23b7x4emlb',
accountRelation: { type: 'set', fields: ['recipient'] },
implements: [
'kjzl6hvfrbw6cakj74rf7d3qjnm3xoydcgx7orzw4bwdmc6kljd04uojuhpef2j',
'kjzl6hvfrbw6c6lxvcf8bc07wjyn29ocoxqn877uia1y86qph79axtdrcuijpeo',
],
id: 'kjzl6hvfrbw6c9rahz7aal75i0ncxkf5wmircmtpz2s34xls8ux1p08ic655oek',
accountRelation: { type: 'list' },
},
},
objects: {
GenericPointAllocation: {
PointsAggregationInterface: {
date: { type: 'datetime', required: true, immutable: false },
points: { type: 'integer', required: true, immutable: false },
recipient: { type: 'did', required: true, immutable: false },
issuer: { type: 'view', viewType: 'documentAccount' },
},
MultiplePoints: {
PointsAllocationInterface: {
points: { type: 'integer', required: true, immutable: false },
recipient: { type: 'did', required: true, immutable: false },
issuer: { type: 'view', viewType: 'documentAccount' },
},
MultiplePointsAllocation: {
PointsInterface: {
points: { type: 'integer', required: true, immutable: false },
recipient: { type: 'did', required: true, immutable: false },
issuer: { type: 'view', viewType: 'documentAccount' },
},
PointsAggregation: {
SimplePointsAggregation: {
date: { type: 'datetime', required: true, immutable: false },
points: { type: 'integer', required: true, immutable: false },
recipient: { type: 'did', required: true, immutable: false },
issuer: { type: 'view', viewType: 'documentAccount' },
},
SinglePoint: {
recipient: { type: 'did', required: true, immutable: false },
issuer: { type: 'view', viewType: 'documentAccount' },
},
SinglePointAllocation: {
recipient: { type: 'did', required: true, immutable: false },
recipient: { type: 'did', required: true, immutable: true },
issuer: { type: 'view', viewType: 'documentAccount' },
},
TotalPoints: {
date: { type: 'datetime', required: true, immutable: false },
SimplePointsAllocation: {
points: { type: 'integer', required: true, immutable: false },
recipient: { type: 'did', required: true, immutable: true },
recipient: { type: 'did', required: true, immutable: false },
issuer: { type: 'view', viewType: 'documentAccount' },
},
},
enums: {},
accountData: {
genericPointAllocationList: { type: 'connection', name: 'GenericPointAllocation' },
multiplePointsAllocationList: { type: 'connection', name: 'MultiplePointsAllocation' },
multiplePointsList: { type: 'connection', name: 'MultiplePoints' },
pointsAggregationList: { type: 'connection', name: 'PointsAggregation' },
recipientOfGenericPointAllocationList: {
pointsAggregationInterfaceList: { type: 'connection', name: 'PointsAggregationInterface' },
pointsAllocationInterfaceList: { type: 'connection', name: 'PointsAllocationInterface' },
pointsInterfaceList: { type: 'connection', name: 'PointsInterface' },
recipientOfPointsAggregationInterfaceList: {
type: 'account',
name: 'GenericPointAllocation',
name: 'PointsAggregationInterface',
property: 'recipient',
},
recipientOfMultiplePointsAllocationList: {
recipientOfPointsAllocationInterfaceList: {
type: 'account',
name: 'MultiplePointsAllocation',
name: 'PointsAllocationInterface',
property: 'recipient',
},
recipientOfMultiplePointsList: {
recipientOfPointsInterfaceList: {
type: 'account',
name: 'MultiplePoints',
name: 'PointsInterface',
property: 'recipient',
},
recipientOfSimplePointsAggregation: {
type: 'account-set',
name: 'SimplePointsAggregation',
property: 'recipient',
},
recipientOfPointsAggregationList: {
recipientOfSimplePointsAggregationList: {
type: 'account',
name: 'PointsAggregation',
name: 'SimplePointsAggregation',
property: 'recipient',
},
recipientOfSinglePointAllocationList: {
recipientOfSimplePointsAllocationList: {
type: 'account',
name: 'SinglePointAllocation',
name: 'SimplePointsAllocation',
property: 'recipient',
},
recipientOfSinglePointList: { type: 'account', name: 'SinglePoint', property: 'recipient' },
recipientOfTotalPoints: { type: 'account-set', name: 'TotalPoints', property: 'recipient' },
recipientOfTotalPointsList: { type: 'account', name: 'TotalPoints', property: 'recipient' },
singlePointAllocationList: { type: 'connection', name: 'SinglePointAllocation' },
singlePointList: { type: 'connection', name: 'SinglePoint' },
totalPoints: { type: 'set', name: 'TotalPoints' },
totalPointsList: { type: 'connection', name: 'TotalPoints' },
simplePointsAggregation: { type: 'set', name: 'SimplePointsAggregation' },
simplePointsAggregationList: { type: 'connection', name: 'SimplePointsAggregation' },
simplePointsAllocationList: { type: 'connection', name: 'SimplePointsAllocation' },
},
}
11 changes: 10 additions & 1 deletion composites/points/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@
* @module points-composite
*/

export { definition } from './definition.js'
import { definition } from './definition.js'

export { definition }
export const SimplePointsAggregationID = definition.models.SimplePointsAggregation.id
export const SimplePointsAllocationID = definition.models.SimplePointsAllocation.id

export type PointsContent = {
recipient: string // DID
points: number
}
2 changes: 1 addition & 1 deletion demo/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"uint8arrays": "^5.0.3"
},
"dependencies": {
"@ceramicnetwork/http-client": "^5.6.0",
"@ceramicnetwork/http-client": "^5.7.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand Down
23 changes: 8 additions & 15 deletions demo/server/src/controllers/multiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const getContextAggregation = async (
const { recipient, context } = req.body

//instantiate a reader
const reader = new PointsReader({
const reader = PointsReader.create({
ceramic,
issuer: ceramic.did!.id,
aggregationModelID,
Expand Down Expand Up @@ -75,7 +75,7 @@ const getTotalAggregation = async (
const { recipient } = req.body

//instantiate a reader
const reader = new PointsReader({
const reader = PointsReader.create({
ceramic,
issuer: ceramic.did!.id,
})
Expand All @@ -101,20 +101,14 @@ const updateContextAggregation = async (
const { ceramic, aggregationModelID } = await getContext()
const { amount, recipient, context } = req.body

//instantiate a writer and reader
const contextWriter = new PointsWriter<ContextAggregationContent>({
//instantiate a writer
const contextWriter = PointsWriter.fromAuthenticated<ContextAggregationContent>({
ceramic,
aggregationModelID,
})

const contextReader = new PointsReader({
ceramic,
issuer: ceramic.did!.id,
aggregationModelID,
})

// load the document for the recipient and context
const doc = await contextReader.loadAggregationDocumentFor([recipient, context])
const doc = await contextWriter.loadAggregationDocumentFor([recipient, context])
if (!doc) {
await contextWriter.setPointsAggregationFor([recipient, context], amount, {
recipient,
Expand Down Expand Up @@ -159,12 +153,11 @@ const updateTotalAggregation = async (
const { ceramic } = await getContext()
const { amount, recipient } = req.body

//instantiate a writer and reader
const totalWriter = new PointsWriter({ ceramic })
const totalReader = new PointsReader({ ceramic, issuer: ceramic.did!.id })
//instantiate a writer
const totalWriter = PointsWriter.fromAuthenticated({ ceramic })

// load the document for the recipient and context
const doc = await totalReader.loadAggregationDocumentFor([recipient])
const doc = await totalWriter.loadAggregationDocumentFor([recipient])
if (!doc) {
await totalWriter.setPointsAggregationFor([recipient], amount, {
recipient,
Expand Down
Loading

0 comments on commit 59afa95

Please sign in to comment.