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

Conversation

rico191013
Copy link
Contributor

@rico191013 rico191013 self-assigned this May 21, 2024
@github-actions github-actions bot added package: wallet/backend Wallet backend implementations type: test Improvements or additions to tests type: source Source changes labels May 21, 2024
@github-actions github-actions bot added package: wallet/frontend Wallet frontend implementations package: boutique/backend Boutique backend implementations labels May 21, 2024
Comment on lines 88 to 109
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')}`
}
}
Copy link
Member

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.

Comment on lines 83 to 85
const grants = await this.rafikiAuthService.listGrantsWithPagination(args)
this.parseIntervals(grants.grants.edges.map((it) => it.node))
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

package.json Outdated
"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

package.json Outdated
"npm": "pnpm",
"yarn": "pnpm",
"node": "^20.12.1"
},
"private": true,
"packageManager": "pnpm@9.1.1"
"packageManager": "pnpm@9.1.2"
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.

Comment on lines 100 to 104
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')}`
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

Comment on lines 88 to 109
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')}`
}
}
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.

sanducb
sanducb previously approved these changes May 30, 2024
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

# Conflicts:
#	package.json
#	packages/boutique/backend/Dockerfile.dev
#	packages/wallet/backend/Dockerfile.dev
#	packages/wallet/frontend/Dockerfile.dev
- Cover repetition
- Cover end and start intervals
@rico191013 rico191013 dismissed stale reviews from sanducb and beniaminmunteanu via 496932e June 3, 2024 12:42
@github-actions github-actions bot removed package: wallet/frontend Wallet frontend implementations package: boutique/backend Boutique backend implementations labels Jun 3, 2024
@github-actions github-actions bot added package: wallet/frontend Wallet frontend implementations package: boutique/backend Boutique backend implementations labels Jun 3, 2024
@rico191013 rico191013 merged commit 2a6f614 into main Jun 5, 2024
15 checks passed
@rico191013 rico191013 deleted the parse-openpayments-intervals branch June 5, 2024 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: boutique/backend Boutique backend implementations package: wallet/backend Wallet backend implementations package: wallet/frontend Wallet frontend implementations type: source Source changes type: test Improvements or additions to tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grants detail page - make it better
4 participants