Skip to content

Commit

Permalink
DRY PaymentsMixin.js
Browse files Browse the repository at this point in the history
  • Loading branch information
SebinSong committed Oct 4, 2023
1 parent 8c9aa43 commit 1c28d16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion frontend/views/containers/payments/PaymentDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default ({
// NOTE: Only for the historical payments, there is 'period'
const { id, period } = this.$route.query
const payment = this.lightningPayment || // TODO: to be re-worked once lightning network is implemented.
this.currentGroupState.payments[id] || await this.getHistoricalPaymentByHashAndPeriod(id, period)
this.currentGroupState.payments[id] || await this.getHistoricalPaymentDetailsByPeriod(period)[id]
if (id) {
sbp('okTurtles.events/emit', SET_MODAL_QUERIES, 'PaymentDetail', { id })
Expand Down
33 changes: 16 additions & 17 deletions frontend/views/containers/payments/PaymentsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ const PaymentsMixin: Object = {
return await this.historicalPeriodStampGivenDate(payment.date)
},

async getAllPaymentPeriods () {
return { ...await this.getHistoricalPaymentPeriods(), ...this.groupPeriodPayments }
async getAllPeriodPayments () {
return { ...await this.getHistoricalPeriodPayments(), ...this.groupPeriodPayments }
},

// Oldest key first.
async getAllSortedPeriodKeys () {
const historicalPaymentPeriods = await this.getHistoricalPaymentPeriods()
const historicalPeriodPayments = await this.getHistoricalPeriodPayments()
return [
...Object.keys(historicalPaymentPeriods).sort(),
...Object.keys(historicalPeriodPayments).sort(),
...this.groupSortedPeriodKeys
]
},
Expand Down Expand Up @@ -68,7 +68,7 @@ const PaymentsMixin: Object = {
},

// ====================
async getHistoricalPaymentPeriods () {
async getHistoricalPeriodPayments () {
const ourArchiveKey = `paymentsByPeriod/${this.ourUsername}/${this.currentGroupId}`
return await sbp('gi.db/archive/load', ourArchiveKey) ?? {}
},
Expand All @@ -77,12 +77,12 @@ const PaymentsMixin: Object = {
const sent = []
const received = []
const todo = cloneDeep(this.ourPayments?.todo ?? [])
const paymentPeriods = await this.getAllPaymentPeriods()
const periodPayments = await this.getAllPeriodPayments()
const sortPayments = (f, l) => f.meta.createdDate > l.meta.createdDate ? 1 : -1

for (const periodStamp of Object.keys(paymentPeriods).sort().reverse()) {
for (const periodStamp of Object.keys(periodPayments).sort().reverse()) {
const paymentsByHash = await this.getPaymentDetailsByPeriod(periodStamp)
const { paymentsFrom } = paymentPeriods[periodStamp]
const { paymentsFrom } = periodPayments[periodStamp]
for (const fromUser of Object.keys(paymentsFrom)) {
for (const toUser of Object.keys(paymentsFrom[fromUser])) {
if (toUser === this.ourUsername || fromUser === this.ourUsername) {
Expand Down Expand Up @@ -110,13 +110,12 @@ const PaymentsMixin: Object = {
const paymentHashes = this.paymentHashesForPeriod(period) || []
detailedPayments = Object.fromEntries(paymentHashes.map(hash => [hash, this.currentGroupState.payments[hash]]))
} else {
const paymentsByPeriod = await this.getHistoricalPaymentPeriods()
const paymentsByPeriod = await this.getHistoricalPeriodPayments()
const paymentHashes = paymentHashesFromPaymentPeriod(paymentsByPeriod[period])
const historicalPaymentDetails = await this.getHistoricalPaymentDetailsByPeriod(period)

const paymentsKey = `payments/${this.ourUsername}/${period}/${this.currentGroupId}`
const payments = await sbp('gi.db/archive/load', paymentsKey) || {}
for (const hash of paymentHashes) {
detailedPayments[hash] = payments[hash]
detailedPayments[hash] = historicalPaymentDetails[hash]
}
}
return detailedPayments
Expand All @@ -133,18 +132,18 @@ const PaymentsMixin: Object = {
}
return payments
},
async getHistoricalPaymentByHashAndPeriod (hash: string, period: string) {
async getHistoricalPaymentDetailsByPeriod (hash: string, period: string) {
const paymentsKey = `payments/${this.ourUsername}/${period}/${this.currentGroupId}`
const payments = await sbp('gi.db/archive/load', paymentsKey) || {}
const paymentDetails = await sbp('gi.db/archive/load', paymentsKey) || {}

return payments[hash]
return paymentDetails
},
async getHaveNeedsSnapshotByPeriod (period: string) {
if (Object.keys(this.groupPeriodPayments).includes(period)) {
return this.groupPeriodPayments[period].haveNeedsSnapshot || []
}

const paymentsByPeriod = await this.getHistoricalPaymentPeriods()
const paymentsByPeriod = await this.getHistoricalPeriodPayments()
return Object.keys(paymentsByPeriod).includes(period)
? paymentsByPeriod[period].haveNeedsSnapshot || []
: []
Expand All @@ -171,7 +170,7 @@ const PaymentsMixin: Object = {
// or an empty object if not found.
// TODOs: rename to getPaymentPeriod, and maybe avoid loading all historical payment periods.
async getPaymentPeriod (period: string) {
return this.groupPeriodPayments[period] ?? (await this.getHistoricalPaymentPeriods())[period] ?? {}
return this.groupPeriodPayments[period] ?? (await this.getHistoricalPeriodPayments())[period] ?? {}
},
// Returns a human-readable description of the time interval identified by a given period stamp.
getPeriodFromStartToDueDate (period) {
Expand Down

0 comments on commit 1c28d16

Please sign in to comment.