-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
110 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { PAID_ACTION_PAYMENT_METHODS } from '@/lib/constants' | ||
import { satsToMsats } from '@/lib/format' | ||
import { notifyInvite } from '@/lib/webPush' | ||
|
||
export const anonable = false | ||
|
||
export const paymentMethods = [ | ||
PAID_ACTION_PAYMENT_METHODS.FEE_CREDIT | ||
] | ||
|
||
export async function getCost ({ id }, { models, me }) { | ||
const invite = await models.invite.findUnique({ where: { id, userId: me.id, revoked: false } }) | ||
if (!invite) { | ||
throw new Error('invite not found') | ||
} | ||
return satsToMsats(invite.gift) | ||
} | ||
|
||
export async function perform ({ id, userId }, { me, cost, tx }) { | ||
const invite = await tx.invite.findUnique({ | ||
where: { id, userId: me.id, revoked: false } | ||
}) | ||
|
||
if (invite.giftedCount >= invite.limit) { | ||
throw new Error('invite limit reached') | ||
} | ||
|
||
// check that user was created in last hour | ||
// check that user did not already redeem an invite | ||
await tx.user.update({ | ||
where: { | ||
id: userId, | ||
inviteId: null, | ||
createdAt: { | ||
gt: new Date(Date.now() - 1000 * 60 * 60) | ||
} | ||
}, | ||
data: { | ||
msats: { | ||
increment: cost | ||
}, | ||
inviteId: id, | ||
referrerId: me.id | ||
} | ||
}) | ||
|
||
return await tx.invite.update({ | ||
where: { id, userId: me.id, giftedCount: { lt: invite.limit }, revoked: false }, | ||
data: { | ||
giftedCount: { | ||
increment: 1 | ||
} | ||
} | ||
}) | ||
} | ||
|
||
export async function nonCriticalSideEffects (_, { me }) { | ||
notifyInvite(me.id) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20241203235457_invite_denormalized_count/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- AlterTable | ||
ALTER TABLE "Invite" ADD COLUMN "giftedCount" INTEGER NOT NULL DEFAULT 0; | ||
|
||
-- denormalize giftedCount | ||
UPDATE "Invite" | ||
SET "giftedCount" = (SELECT COUNT(*) FROM "users" WHERE "users"."inviteId" = "Invite".id) | ||
WHERE "Invite"."id" = "Invite".id; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters