Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse open-payments intervals #1355

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
"typescript": "^5.4.5"
},
"engines": {
"pnpm": "^9.1.1",
"pnpm": "^9.1.2",
"npm": "pnpm",
"yarn": "pnpm",
"node": "^20.12.1"
},
"private": true,
"packageManager": "pnpm@9.1.1"
"packageManager": "pnpm@9.1.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't deploy pipeline need to adapt to this as well?
Also i'd prefer keeping such upgrades outside of feature PR's like this one

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also agree that we should keep these updates outside of feature PRs unless it's necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I didn't make this change, CI would encounter an error that the pnpm version should be updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i saw this

Should still be a separate PR for that merged b4 this one, but ok

}
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.1 --activate
RUN corepack prepare pnpm@9.1.2 --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.1 --activate
RUN corepack prepare pnpm@9.1.2 --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
30 changes: 28 additions & 2 deletions packages/wallet/backend/src/grant/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { 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 +80,32 @@ export class GrantService implements IGrantService {
args.filter = { identifier: { in: identifiers } }
}

return await this.rafikiAuthService.listGrantsWithPagination(args)
const grants = await this.rafikiAuthService.listGrantsWithPagination(args)
this.parseIntervals(grants.grants.edges.map((it) => it.node))
rico191013 marked this conversation as resolved.
Show resolved Hide resolved
return grants
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage of the non-mutating function would be sth like:
(Not tested)

Suggested change
const grants = await this.rafikiAuthService.listGrantsWithPagination(args)
this.parseIntervals(grants.grants.edges.map((it) => it.node))
return grants
const grants = await this.rafikiAuthService.listGrantsWithPagination(args)
const parsedGrants = this.parseIntervals(grants.grants.edges.map((it) => it.node))
return {
...grants,
grants: {
...grants.grants,
edges: grants.grants.edges.map((edge, index) => ({ ...edge, node: parsedGrants[index] }))
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be harder to read than the current one.
and it would have more time complexity because we have one more for loop here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anything that does not mutate data will implicitly have 1 extra loop

}

private parseIntervals(grants: Grant[]) {
for (const grant of grants)
for (const accessElement of grant.access)
if (accessElement.limits?.interval) {
const time = accessElement.limits?.interval.split('/')
rico191013 marked this conversation as resolved.
Show resolved Hide resolved
rico191013 marked this conversation as resolved.
Show resolved Hide resolved
const duration = moment.duration(time[2])
rico191013 marked this conversation as resolved.
Show resolved Hide resolved
const date = moment(time[1])
const months = duration.asMonths()
if (months === 1)
accessElement.limits!.interval = `every month from ${date.format('ddd')}`
else if (months > 1 && months < 12)
accessElement.limits!.interval = `every ${months} month from ${date.format('ddd')}`
else if (months === 12)
accessElement.limits!.interval = `every year from ${date.format('ddd')}`
else if (months > 12) {
rico191013 marked this conversation as resolved.
Show resolved Hide resolved
const years = duration.asYears()
accessElement.limits!.interval = `every ${years} year from ${date.format('ddd')}`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two branches look like they can be merged into one, WDYT? @rico191013

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

their messages are different!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the choice is between having 2 branches or having string interpolation in just one.
I'm fine with both

} else if (months < 1 && months > 0) {
const days = duration.asDays()
accessElement.limits!.interval = `every ${days} days from ${date.format('ddd')}`
rico191013 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Copy link
Member

@beniaminmunteanu beniaminmunteanu May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about an implementation of this method that does not mutate the grants list?
(by using .map)
(This would also imply changes on the usage of the function)
(did not test it out)

Suggested change
private parseIntervals(grants: Grant[]) {
for (const grant of grants)
for (const accessElement of grant.access)
if (accessElement.limits?.interval) {
const time = accessElement.limits?.interval.split('/')
const duration = moment.duration(time[2])
const date = moment(time[1])
const months = duration.asMonths()
if (months === 1)
accessElement.limits!.interval = `every month from ${date.format('ddd')}`
else if (months > 1 && months < 12)
accessElement.limits!.interval = `every ${months} month from ${date.format('ddd')}`
else if (months === 12)
accessElement.limits!.interval = `every year from ${date.format('ddd')}`
else if (months > 12) {
const years = duration.asYears()
accessElement.limits!.interval = `every ${years} year from ${date.format('ddd')}`
} else if (months < 1 && months > 0) {
const days = duration.asDays()
accessElement.limits!.interval = `every ${days} days from ${date.format('ddd')}`
}
}
private parseIntervals(grants: Grant[]): Grant[] {
return grants.map(grant => {
const access = grant.access.map(accessElement => {
if (accessElement.limits?.interval) {
const time = accessElement.limits.interval.split('/')
const duration = moment.duration(time[2])
const date = moment(time[1])
const months = duration.asMonths()
let interval: string;
if (months === 1) {
interval = `every month from ${date.format('ddd')}`;
} else if (months > 1 && months < 12) {
interval = `every ${months} month from ${date.format('ddd')}`;
} else if (months === 12) {
interval = `every year from ${date.format('ddd')}`;
} else if (months > 12) {
const years = duration.asYears();
interval = `every ${years} year from ${date.format('ddd')}`;
} else if (months < 1 && months > 0) {
const days = duration.asDays();
interval = `every ${days} days from ${date.format('ddd')}`;
}
return {
...accessElement,
limits: {
...accessElement.limits,
interval: interval,
},
};
}
return accessElement;
});
return { ...grant, access };
});
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you find it decreases readability, I guess we could settle for mutating the grants in the parse method as well.
Up to you & the others

Copy link

@sanducb sanducb May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beniaminmunteanu I think that your suggested changes do not decrease readability, but the else if branches do so. I suggested a small change to those branches here, but I think that this can be somehow simplified even more.

}
}
20 changes: 19 additions & 1 deletion packages/wallet/backend/tests/grant/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,25 @@ 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({
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: 'R/2016-08-23T08:00:00Z/P1M'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the mocks are changed but there are no tests to check the interval transformation?

Copy link
Contributor Author

@rico191013 rico191013 Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It covers in the list pagination test

}
}
]
},
{
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.1 --activate
RUN corepack prepare pnpm@9.1.2 --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.

Loading