Skip to content

Commit

Permalink
feat: parse open-payments intervals (#1355)
Browse files Browse the repository at this point in the history
* parse open-payments intervals

* Fix pnpm version conflict

* - Cover hours and minutes
- Cover repetition
- Cover end and start intervals

* fix format

* Fix pnpm version conflict

* Fix Test

* Simplify the logic
  • Loading branch information
rico191013 authored Jun 5, 2024
1 parent a33ebc0 commit 2a6f614
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
"typescript": "^5.4.5"
},
"engines": {
"pnpm": "^9.1.3",
"pnpm": "^9.1.4",
"npm": "pnpm",
"yarn": "pnpm",
"node": "^20.12.1"
},
"private": true,
"packageManager": "pnpm@9.1.3"
"packageManager": "pnpm@9.1.4"
}
2 changes: 1 addition & 1 deletion packages/boutique/backend/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WORKDIR /home/testnet

# Install PNPM
RUN corepack enable
RUN corepack prepare pnpm@9.1.3 --activate
RUN corepack prepare pnpm@9.1.4 --activate

# Copy lockfile, NVM and NPM configuration to the working directory
COPY pnpm-lock.yaml .nvmrc .npmrc ./
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/backend/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WORKDIR /home/testnet

# Install PNPM
RUN corepack enable
RUN corepack prepare pnpm@9.1.3 --activate
RUN corepack prepare pnpm@9.1.4 --activate

# Copy lockfile, NVM and NPM configuration to the working directory
COPY pnpm-lock.yaml .nvmrc .npmrc ./
Expand Down
1 change: 1 addition & 0 deletions packages/wallet/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ioredis": "^5.4.1",
"iron-session": "^6.3.1",
"knex": "^3.1.0",
"moment": "^2.30.1",
"node-cache": "^5.1.2",
"objection": "^3.1.4",
"pg": "^8.11.5",
Expand Down
71 changes: 68 additions & 3 deletions packages/wallet/backend/src/grant/service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { GetGrantsQueryVariables, Grant } from '@/rafiki/auth/generated/graphql'
import {
Access,
GetGrantsQueryVariables,
Grant
} from '@/rafiki/auth/generated/graphql'
import { RafikiAuthService } from '@/rafiki/auth/service'
import { WalletAddressService } from '@/walletAddress/service'
import { Forbidden } from '@shared/backend'

import moment from 'moment'
interface IGrantService {
getGrantByInteraction: (
userId: string,
Expand Down Expand Up @@ -80,6 +84,67 @@ export class GrantService implements IGrantService {
args.filter = { identifier: { in: identifiers } }
}

return await this.rafikiAuthService.listGrantsWithPagination(args)
const grants = await this.rafikiAuthService.listGrantsWithPagination(args)
grants.grants.edges.forEach((edge) => {
edge.node.access = this.parseIntervals(edge.node.access)
})
return grants
}

private parseIntervals(access: Access[]): Access[] {
return access.map((accessElement) => {
if (accessElement.limits?.interval) {
return {
...accessElement,
limits: {
...accessElement.limits,
intervalHR: this.transformFromIntervalToHR(
accessElement.limits.interval
)
}
}
}
return accessElement
})
}

private transformFromIntervalToHR(interval: string): string {
const time = interval.split('/')
const isEnd = time[1].startsWith('P')
const duration = moment.duration(time.find((it) => it.startsWith('P')))
const date = moment(time.find((it) => it.endsWith('Z')))
const repetition = time.find((it) => it.startsWith('R'))?.[1]

if (repetition === '0')
return `${isEnd ? 'Until' : 'From'} ${date.format('MMMM Do YYYY')} with no repetition`

if (repetition === undefined)
return `${this.processDuration(duration)} ${isEnd ? 'until' : 'from'} ${date.format('MMMM Do YYYY')}`

return `${repetition} times ${this.processDuration(duration)} ${isEnd ? 'until' : 'from'} ${date.format('MMMM Do YYYY')}`
}
private processDuration(duration: moment.Duration) {
const years =
duration.years() !== 0
? `${duration.years()} ${duration.years() > 1 ? 'years' : 'year'}, `
: ''
const months =
duration.months() !== 0
? `${duration.months()} ${duration.months() > 1 ? 'months' : 'month'}, `
: ''
const days =
duration.days() !== 0
? `${duration.days()} ${duration.days() > 1 ? 'days' : 'day'}, `
: ''
const hours =
duration.hours() !== 0
? `${duration.hours()} ${duration.hours() > 1 ? 'hours' : 'hour'}, `
: ''
const minutes =
duration.minutes() !== 0
? `${duration.minutes()} ${duration.minutes() > 1 ? 'minutes' : 'minute'}`
: ''

return `Every ${years}${months}${days}${hours}${minutes}`.replace(/, $/, '')
}
}
24 changes: 22 additions & 2 deletions packages/wallet/backend/tests/grant/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,32 @@ describe('Grant Service', () => {
: jest.fn(),
getGrantByInteraction: jest.fn().mockReturnValue({
id: 'grant',
access: [{ identifier: faker.internet.url() }]
access: [
{
identifier: faker.internet.url(),
limits: {
receiver: null,
debitAmount: {
value: '12425',
assetCode: 'USD',
assetScale: 2
},
receiveAmount: {
value: '12300',
assetCode: 'USD',
assetScale: 2
},
interval: 'R/2016-08-23T08:00:00Z/P1M'
}
}
]
}),
listGrants: jest.fn().mockReturnValue(mockedListGrant),
listGrantsWithPagination: jest.fn().mockReturnValue({
grants: {
edges: mockedListGrant.map((grant) => ({ node: grant })),
edges: JSON.parse(
JSON.stringify(mockedListGrant.map((grant) => ({ node: grant })))
),
pageInfo: {
hasNextPage: false,
hasPreviousPage: false
Expand Down
40 changes: 39 additions & 1 deletion packages/wallet/backend/tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,50 @@ export const mockedListGrant = [
{
id: faker.string.uuid(),
client: faker.lorem.slug(),
state: 'APPROVED'
state: 'APPROVED',
access: [
{
identifier: faker.internet.url(),
limits: {
receiver: null,
debitAmount: {
value: '12425',
assetCode: 'USD',
assetScale: 2
},
receiveAmount: {
value: '12300',
assetCode: 'USD',
assetScale: 2
},
interval: 'R5/2008-03-01T13:00:00Z/P1Y2M10DT2H30M'
}
}
]
},
{
id: faker.string.uuid(),
client: faker.lorem.slug(),
state: 'FINALIZED',
access: [
{
identifier: faker.internet.url(),
limits: {
receiver: null,
debitAmount: {
value: '12425',
assetCode: 'USD',
assetScale: 2
},
receiveAmount: {
value: '12300',
assetCode: 'USD',
assetScale: 2
},
interval: 'R/2016-08-23T08:00:00Z/P2Y'
}
}
],
finalizationReason: 'REJECTED'
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/frontend/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN apt-get update && \

# Install PNPM
RUN corepack enable
RUN corepack prepare pnpm@9.1.3 --activate
RUN corepack prepare pnpm@9.1.4 --activate

# Copy lockfile, NVM and NPM configuration to the working directory
COPY pnpm-lock.yaml .nvmrc .npmrc ./
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a6f614

Please sign in to comment.