Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions src/lib/grant-return-coins.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import "server-only"
import { lockUserCredit, reconcileGrant } from "@/lib/currency"
import { HcbGrantCardStatus, LedgerKind, Phase } from "@/lib/enums"
import { HcbGrantCardStatus, LedgerKind, Phase, PhaseStatus } from "@/lib/enums"
import { grantCardCapUsd } from "@/lib/grant-card-policy"
import { isWeekOneStarter } from "@/lib/grant-cards"
import { readCardGrantSpend } from "@/lib/hcb"
import { PRE_ACTIVATION_STATUS } from "@/lib/hcb-buffer-topups"
import { MAKER_RETURN_STATUS, PRE_ACTIVATION_STATUS } from "@/lib/hcb-buffer-topups"
import db from "@/lib/prisma8"

/**
Expand All @@ -31,11 +31,10 @@ export interface GrantReturnCoinsResult {
}

/**
* The HCB grant statuses that mean the maker handed the balance back, and so earn coins. A converted grant reads differently and is left out on purpose: its money goes out as a reimbursement.
*
* Empty until the statuses HCB reports are read off a live run, so nothing is paid on a guess.
* The HCB grant statuses that mean the maker handed the balance back, and so earn coins. A conversion to a reimbursement is
* requested by someone other than the maker, so it never gets this status and is left out on purpose.
*/
export const PLAIN_RETURN_STATUSES: readonly string[] = []
export const PLAIN_RETURN_STATUSES: readonly string[] = [MAKER_RETURN_STATUS]

/**
* Whether a returned card earns coins. A grant cancelled before activation always does: the site counted it as issued, and
Expand Down Expand Up @@ -79,7 +78,7 @@ export async function syncGrantReturnCoins(input: { dryRun: boolean }): Promise<
projectIds.length === 0
? []
: await db.orm.public.ThemeProject.where(project => project.id.in(projectIds))
.select("grantUsd", "id", "starterProjectId", "tier", "title")
.select("designStatus", "grantUsd", "id", "starterProjectId", "tier", "title")
.all()
const declinedRows =
projectIds.length === 0
Expand All @@ -96,6 +95,10 @@ export async function syncGrantReturnCoins(input: { dryRun: boolean }): Promise<
const skip = (reason: string): void => {
result.skipped.push({ reason, themeProjectId: project.id })
}
if (project.designStatus !== PhaseStatus.approved) {
skip("The design is not approved, so nothing is paid")
continue
}
if (declined.has(project.id)) {
skip("Parts funding was declined, so nothing was taken out of the cap")
continue
Expand Down
14 changes: 14 additions & 0 deletions src/lib/hcb-buffer-topups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
fundsReturn,
grantReturn,
grantReturnedAt,
MAKER_RETURN_STATUS,
memoKind,
returnedBy,
returnedById,
returnHcbStatus,
selectBufferTopUps,
} from "@/lib/hcb-buffer-topups"

Expand Down Expand Up @@ -171,3 +173,15 @@ describe("returnedById", () => {
expect(returnedById("user_1", null)).toBe("unknown")
})
})

describe("returnHcbStatus", () => {
test("a maker's own return is payable, and an admin cancel or conversion keeps HCB's status", () => {
expect(returnHcbStatus({ grantStatus: "canceled", makerRequested: true, preActivation: false })).toBe(
MAKER_RETURN_STATUS,
)
expect(returnHcbStatus({ grantStatus: "canceled", makerRequested: false, preActivation: false })).toBe("canceled")
expect(returnHcbStatus({ grantStatus: "canceled", makerRequested: false, preActivation: true })).toBe(
"canceled before activation",
)
})
})
25 changes: 23 additions & 2 deletions src/lib/hcb-buffer-topups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ export interface GrantReturn {
/** HCB's memo for a balance returned without a cancel, as an admin cancelling a grant before it was activated sees it. */
export const GRANT_FUNDS_RETURN_MEMO_PREFIX = "Return of funds from grant to "

/** The status a grant's row takes when the maker themselves handed the balance back. Only a maker's own return is paid. */
export const MAKER_RETURN_STATUS = "canceled by maker"

/**
* The status a returned grant's row takes. A grant cancelled before activation and a maker's own return are payable.
* Anything else, such as an admin cancel or a conversion to a reimbursement, keeps HCB's own status and is held.
*/
export function returnHcbStatus(input: {
grantStatus: string | null
makerRequested: boolean
preActivation: boolean
}): string | null {
if (input.preActivation) return PRE_ACTIVATION_STATUS
if (input.makerRequested && input.grantStatus === "canceled") return MAKER_RETURN_STATUS
return input.grantStatus
}

/** The status a grant's row takes when HCB cancelled it before its card was activated, so nothing was ever spent. */
export const PRE_ACTIVATION_STATUS = "canceled before activation"

Expand Down Expand Up @@ -321,8 +338,12 @@ export async function syncBufferTopUps(input: { dryRun: boolean }): Promise<Buff
const beforeActivation = grant.status === "canceled" && grant.cardId === null ? fundsReturn(read.rows) : null
const found: GrantReturn | null =
grantReturn(read.rows) ?? (beforeActivation === null ? null : { disbursement: beforeActivation, expired: false })
const hcbStatus =
beforeActivation !== null && found?.disbursement === beforeActivation ? PRE_ACTIVATION_STATUS : grant.status
const hcbStatus = returnHcbStatus({
grantStatus: grant.status,
makerRequested:
found !== null && !found.expired && returnedById(found.disbursement.senderId, grant.ownerId) === "maker",
preActivation: beforeActivation !== null && found?.disbursement === beforeActivation,
})
if (found?.disbursement.createdAt)
returnsSeen.push({
byId: returnedById(found.disbursement.senderId, grant.ownerId),
Expand Down
1 change: 1 addition & 0 deletions src/lib/hcb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function grant(overrides: Partial<HcbCardGrantSummary> = {}): HcbCardGrantSummar
email: "maker@example.com",
id: "cdg_1",
merchantLock: [],
ownerId: null,
purpose: "Half Life parts – HL:prj_123",
status: "active",
...overrides,
Expand Down
Loading