toBatch => lodash's chunk

This commit is contained in:
James Grugett 2022-10-12 16:12:39 -05:00
parent af3c2ade08
commit 1e00af259b
2 changed files with 5 additions and 14 deletions

View File

@ -38,11 +38,3 @@ export function groupConsecutive<T, U>(xs: T[], key: (x: T) => U) {
result.push(curr)
return result
}
export function toBatches<T>(xs: T[], batchSize: number) {
const result = []
for (let i = 0; i < xs.length; i += batchSize) {
result.push(xs.slice(i, i + batchSize))
}
return result
}

View File

@ -1,13 +1,12 @@
import * as admin from 'firebase-admin'
import fetch from 'node-fetch'
import { FieldValue, Transaction } from 'firebase-admin/firestore'
import { chunk, groupBy, mapValues, sumBy } from 'lodash'
import { Contract } from '../../common/contract'
import { PrivateUser, User } from '../../common/user'
import { Group } from '../../common/group'
import { Post } from '../../common/post'
import { FieldValue, Transaction } from 'firebase-admin/firestore'
import { toBatches } from 'common/util/array'
export const log = (...args: unknown[]) => {
console.log(`[${new Date().toISOString()}]`, ...args)
@ -215,11 +214,11 @@ export const payUsersMultipleTransactions = async (
}[]
) => {
const mergedPayouts = checkAndMergePayouts(payouts)
const batchedPayouts = toBatches(mergedPayouts, 500)
const payoutChunks = chunk(mergedPayouts, 500)
for (const batch of batchedPayouts) {
for (const payoutChunk of payoutChunks) {
await firestore.runTransaction(async (transaction) => {
for (const { userId, payout, deposit } of batch) {
for (const { userId, payout, deposit } of payoutChunk) {
updateUserBalance(transaction, userId, payout, deposit)
}
})