-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from compsci-adl/feat/drive-link
feat: Add drive link
- Loading branch information
Showing
11 changed files
with
113 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ SQUARE_LOCATION_ID= | |
|
||
# Redis | ||
REDIS_URI= | ||
|
||
# Club Resources OneDrive link | ||
NEXT_PUBLIC_DRIVE_LINK= |
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
2 changes: 1 addition & 1 deletion
2
src/app/api/check-user-exists/route.ts → src/app/api/user-existence/route.ts
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
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,11 @@ | ||
import { db } from '@/db'; | ||
import { memberTable } from '@/db/schema'; | ||
import { eq } from 'drizzle-orm'; | ||
|
||
export const checkUserExists = async (clerkUserId: string) => { | ||
const existingUser = await db | ||
.select({ count: memberTable.id }) | ||
.from(memberTable) | ||
.where(eq(memberTable.clerkId, clerkUserId)); | ||
return existingUser.length > 0; | ||
}; |
12 changes: 2 additions & 10 deletions
12
src/db/queries.ts → src/server/update-member-expiry-date.ts
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,43 @@ | ||
import { db } from '@/db'; | ||
import { memberTable } from '@/db/schema'; | ||
import { redisClient } from '@/lib/redis'; | ||
import { squareClient } from '@/lib/square'; | ||
import { eq } from 'drizzle-orm'; | ||
import { updateMemberExpiryDate } from './update-member-expiry-date'; | ||
|
||
export const verifyMembershipPayment = async (clerkId: string) => { | ||
// Get user's membership expiry date from the database | ||
const [{ membershipExpiresAt }] = await db | ||
.select({ | ||
id: memberTable.id, | ||
membershipExpiresAt: memberTable.membershipExpiresAt, | ||
}) | ||
.from(memberTable) | ||
.where(eq(memberTable.clerkId, clerkId)); | ||
// If membership expiry date exists, return the existing date | ||
if (membershipExpiresAt) { | ||
return { paid: true as const, membershipExpiresAt }; | ||
} | ||
|
||
const paymentId = await redisClient.hGet(`payment:membership:${clerkId}`, 'paymentId'); | ||
if (!paymentId) { | ||
// Membership payment for the user does not exist | ||
return { paid: false as const }; | ||
} | ||
|
||
const resp = await squareClient.checkoutApi.retrievePaymentLink(paymentId); | ||
const respFields = resp.result; | ||
if (!respFields.paymentLink || respFields.paymentLink.id !== paymentId) { | ||
// Payment has not been made | ||
return { paid: false as const }; | ||
} | ||
|
||
// Set expiry date to be the January 1st of the following year | ||
const expiryDate = await updateMemberExpiryDate(clerkId, 'clerkId'); | ||
|
||
// Delete key from Redis since it is no longer needed | ||
await redisClient.del(`payment:membership:${clerkId}`); | ||
|
||
return { paid: true as const, membershipExpiresAt: expiryDate }; | ||
}; | ||
export type MembershipPayment = Awaited<ReturnType<typeof verifyMembershipPayment>>; |