feat(mp): add Mercado Pago reauthorization notice and status card components

- Implemented MpReauthNotice component to notify users when reauthorization is needed.
- Created MpStatusCard component to display the current status of the Mercado Pago account.
- Added utility functions and constants for handling Mercado Pago status and messages.
- Introduced hooks for managing the connection and disconnection of Mercado Pago accounts.
- Developed a disconnect confirmation modal for user actions.
- Added tests for the new functionality and utility functions.
- Defined schemas for Mercado Pago account status and callback reasons in shared package.
This commit is contained in:
Jose Selesan
2026-10-07 12:00:38 -03:00
parent e03f510874
commit 21a4c69f38
64 changed files with 3154 additions and 425 deletions

View File

@@ -0,0 +1,56 @@
-- Mercado Pago OAuth (Authorization Code + PKCE). Replaces the simulated
-- payment setup, where a fake access token was stored in plaintext.
-- CreateEnum
CREATE TYPE "MpAccountStatus" AS ENUM ('ACTIVE', 'REQUIRES_REAUTH', 'REVOKED');
-- CreateTable
CREATE TABLE "mp_accounts" (
"id" TEXT NOT NULL,
"owner_id" TEXT NOT NULL,
"mp_user_id" TEXT NOT NULL,
"access_token_enc" TEXT NOT NULL,
"refresh_token_enc" TEXT NOT NULL,
"expires_at" TIMESTAMP(3) NOT NULL,
"scope" TEXT,
"live_mode" BOOLEAN NOT NULL DEFAULT false,
"status" "MpAccountStatus" NOT NULL DEFAULT 'ACTIVE',
"connected_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "mp_accounts_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "mp_oauth_states" (
"state" TEXT NOT NULL,
"owner_id" TEXT NOT NULL,
"code_verifier" TEXT NOT NULL,
"return_to" TEXT NOT NULL,
"expires_at" TIMESTAMP(3) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "mp_oauth_states_pkey" PRIMARY KEY ("state")
);
-- CreateIndex
CREATE INDEX "mp_accounts_status_expiresAt_idx" ON "mp_accounts"("status", "expires_at");
-- CreateIndex
CREATE UNIQUE INDEX "mp_accounts_ownerId_key" ON "mp_accounts"("owner_id");
-- CreateIndex
CREATE UNIQUE INDEX "mp_accounts_mpUserId_key" ON "mp_accounts"("mp_user_id");
-- CreateIndex
CREATE INDEX "mp_oauth_states_expiresAt_idx" ON "mp_oauth_states"("expires_at");
-- AddForeignKey
ALTER TABLE "mp_accounts" ADD CONSTRAINT "mp_accounts_ownerId_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- DropTable
DROP TABLE "merchant_accounts";
-- DropEnum
DROP TYPE "PaymentProvider";

View File

@@ -18,7 +18,7 @@ model User {
orgMemberships Member[]
orgInvitations Invitation[] @relation("InvitedBy")
waitlist WaitlistEntry[]
merchantAccount MerchantAccount?
mpAccount MpAccount?
@@map("users")
}

View File

@@ -182,22 +182,49 @@ model GroupPreRegistration {
@@map("group_pre_registrations")
}
enum PaymentProvider {
MERCADO_PAGO
STRIPE
enum MpAccountStatus {
ACTIVE
REQUIRES_REAUTH
REVOKED
}
model MerchantAccount {
id String @id @default(cuid())
userId String @unique(map: "merchant_accounts_userId_key") @map("user_id")
provider PaymentProvider
accessToken String? @map("access_token")
sandbox Boolean @default(true)
connectedAt DateTime @default(now()) @map("connected_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
// Mercado Pago account linked by a teacher through OAuth (Authorization Code + PKCE).
// Tokens are stored encrypted (AES-256-GCM); there is a single active account per
// teacher and a Mercado Pago user cannot be linked to two teachers.
model MpAccount {
id String @id @default(cuid())
ownerId String @unique(map: "mp_accounts_ownerId_key") @map("owner_id")
mpUserId String @unique(map: "mp_accounts_mpUserId_key") @map("mp_user_id")
accessTokenEnc String @map("access_token_enc")
refreshTokenEnc String @map("refresh_token_enc")
expiresAt DateTime @map("expires_at")
scope String?
liveMode Boolean @default(false) @map("live_mode")
status MpAccountStatus @default(ACTIVE)
connectedAt DateTime @default(now()) @map("connected_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade, map: "merchant_accounts_userId_fkey")
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade, map: "mp_accounts_ownerId_fkey")
@@map("merchant_accounts")
// The daily refresh job looks for ACTIVE accounts expiring within 30 days.
@@index([status, expiresAt], map: "mp_accounts_status_expiresAt_idx")
@@map("mp_accounts")
}
// Pending OAuth authorization: the `state` sent to Mercado Pago plus the PKCE
// `code_verifier`. The `state` is the primary key because consuming it (deleting
// the row) is what makes it single use.
model MpOAuthState {
state String @id
ownerId String @map("owner_id")
codeVerifier String @map("code_verifier")
// Constrained to 'onboarding' | 'settings'; decides where the callback lands.
returnTo String @map("return_to")
expiresAt DateTime @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
// Expired rows are swept on every new authorization request.
@@index([expiresAt], map: "mp_oauth_states_expiresAt_idx")
@@map("mp_oauth_states")
}