Spaces:
Running
Running
File size: 18,446 Bytes
d6596e1 c637b42 46ab86e c637b42 f237798 6068938 f237798 d6596e1 f237798 6068938 515e3fb 6068938 ff96ce0 f237798 b90c07a f237798 6068938 f237798 2f42945 f237798 6068938 f237798 2f42945 f237798 6068938 46ab86e f237798 6068938 f237798 6068938 f237798 c637b42 f237798 c637b42 f237798 c637b42 f237798 c637b42 f237798 c637b42 f237798 6068938 2f42945 6068938 46ab86e 6068938 46ab86e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | import { getSupabaseAdmin, getSupabaseClientForRequest } from '../_shared/supabaseAdmin.js';
import { isOrderPaidOnCashfree } from './cashfreeService.js';
import { issueCertificate } from './certificationSystem.js';
import { sendCertificateIssuedEmail } from './emailService.js';
import { backendLogger as logger } from '../_shared/backendLogger.js';
import type {
ApprovedPaymentContext,
CertificatePayload,
CertificationTrack,
PaymentApprovalInput,
PaymentReviewStatus,
PaymentRequest,
PaymentRequestCreateInput,
PaymentRequestStatus,
} from '../_types.js';
const PAYMENT_REQUESTS_TABLE = 'payment_requests';
function parseBoolean(value: string | undefined, fallback: boolean): boolean {
if (value === undefined) return fallback;
return value.toLowerCase() === 'true';
}
export function isPaywallEnabled(): boolean {
return parseBoolean(process.env.CERTIFICATE_PAYWALL_ENABLED, true);
}
export function getCertificateFeeInr(track?: CertificationTrack): number {
if (track === 'ytcert') {
const parsed = Number(process.env.YTCERT_FEE_INR || 59);
return Number.isFinite(parsed) && parsed > 0 ? Math.round(parsed) : 59;
}
if (track === 'quickcert') {
const parsed = Number(process.env.QUICKCERT_FEE_INR || 79);
return Number.isFinite(parsed) && parsed > 0 ? Math.round(parsed) : 79;
}
const parsed = Number(process.env.VERIFIEDCERT_FEE_INR || process.env.CERTIFICATE_FEE_INR || 149);
return Number.isFinite(parsed) && parsed > 0 ? Math.round(parsed) : 149;
}
function mapPaymentRequest(row: any): PaymentRequest {
return {
id: row.id,
userId: row.user_id,
txnId: row.txn_id,
skill: row.skill,
projectTitle: row.project_title,
status: row.status as PaymentRequestStatus,
certificateIssued: Boolean(row.certificate_issued),
createdAt: row.created_at,
updatedAt: row.updated_at,
track: (row.track || 'verifiedcert') as CertificationTrack,
};
}
async function findPaymentRequestForReview(input: {
paymentRequestId?: string;
txnId?: string;
authToken?: string | null;
}): Promise<{ id: string; certificate_issued: boolean }> {
const supabase = getSupabaseClientForRequest(input.authToken);
if (!input.paymentRequestId && !input.txnId) {
throw new Error('paymentRequestId or txnId is required for payment review.');
}
let query = supabase.from(PAYMENT_REQUESTS_TABLE).select('id, certificate_issued').limit(1);
if (input.paymentRequestId) {
query = query.eq('id', input.paymentRequestId);
}
if (input.txnId) {
query = query.eq('txn_id', input.txnId);
}
const { data, error } = await query.maybeSingle();
if (error || !data) {
throw new Error('Payment request not found for review.');
}
return data;
}
function mapCertificate(row: any): CertificatePayload {
return {
certificateId: row.metadata?.certificate_id || row.id,
userId: row.user_id,
name: row.metadata?.username || row.username || 'Student',
skill: row.topic,
projectTitle: row.project_title || '',
githubUrl: row.metadata?.github_repo_url || row.metadata?.github_url || '',
proofUrl: row.metadata?.live_demo_url || row.metadata?.proof_url || '',
issuedAt: row.issued_at,
track: (row.metadata?.certification_track || 'verifiedcert') as CertificationTrack,
};
}
async function findCertificateByPaymentRequestId(
paymentRequestId: string,
authToken?: string | null
): Promise<CertificatePayload | null> {
const supabase = getSupabaseClientForRequest(authToken);
const { data, error } = await supabase
.from('certificates')
.select('id, user_id, topic, project_title, issued_at, metadata')
.eq('metadata->>payment_request_id', paymentRequestId)
.order('issued_at', { ascending: false })
.limit(1)
.maybeSingle();
if (error || !data) {
return null;
}
return mapCertificate(data);
}
export async function submitPaymentRequest(input: PaymentRequestCreateInput & { id?: string }, authToken?: string | null): Promise<PaymentRequest> {
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
const supabase = (!authToken && serviceRoleKey) ? getSupabaseAdmin() : getSupabaseClientForRequest(authToken);
const { data, error } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.insert({
id: input.id,
user_id: input.userId,
txn_id: input.txnId,
skill: input.skill,
project_title: input.projectTitle,
track: input.track || 'verifiedcert',
status: 'pending',
certificate_issued: false,
})
.select('*')
.single();
if (!error && data) {
return mapPaymentRequest(data);
}
if (error?.code === '23505') {
const { data: existing, error: existingError } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.select('*')
.eq('txn_id', input.txnId)
.maybeSingle();
if (existingError || !existing) {
throw new Error(`Transaction ID already exists and cannot be resolved: ${error.message}`);
}
if (existing.user_id !== input.userId) {
throw new Error('This transaction ID is already used by another account.');
}
return mapPaymentRequest(existing);
}
throw new Error(`Failed to submit payment request: ${error?.message || 'unknown error'}`);
}
export async function getLatestPaymentRequest(userId: string): Promise<PaymentRequest | null> {
const supabase = getSupabaseClientForRequest(null);
const { data, error } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.select('*')
.eq('user_id', userId)
.order('created_at', { ascending: false })
.limit(1)
.maybeSingle();
if (error || !data) {
return null;
}
return mapPaymentRequest(data);
}
export async function getPaymentRequestForUser(input: {
userId: string;
paymentRequestId?: string;
txnId?: string;
skill?: string;
projectTitle?: string;
}, authToken?: string | null): Promise<PaymentRequest | null> {
const supabase = getSupabaseClientForRequest(authToken);
let query = supabase
.from(PAYMENT_REQUESTS_TABLE)
.select('*')
.eq('user_id', input.userId)
.order('created_at', { ascending: false })
.limit(1);
if (input.paymentRequestId) {
query = query.eq('id', input.paymentRequestId);
} else {
if (input.txnId) {
query = query.eq('txn_id', input.txnId);
}
if (input.skill) {
query = query.eq('skill', input.skill);
}
if (input.projectTitle) {
query = query.eq('project_title', input.projectTitle);
}
}
const { data, error } = await query.maybeSingle();
if (error || !data) {
return null;
}
return mapPaymentRequest(data);
}
async function resolvePaymentRequest(input: PaymentApprovalInput, authToken?: string | null): Promise<PaymentRequest> {
const supabase = getSupabaseClientForRequest(authToken);
if (input.paymentRequestId) {
const { data, error } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.select('*')
.eq('id', input.paymentRequestId)
.eq('user_id', input.userId)
.maybeSingle();
if (error || !data) {
throw new Error('Payment request not found for this user.');
}
return mapPaymentRequest(data);
}
let query = supabase
.from(PAYMENT_REQUESTS_TABLE)
.select('*')
.eq('user_id', input.userId)
.order('created_at', { ascending: false })
.limit(1);
if (input.txnId) {
query = query.eq('txn_id', input.txnId);
}
if (input.skill) {
query = query.eq('skill', input.skill);
}
if (input.projectTitle) {
query = query.eq('project_title', input.projectTitle);
}
const { data, error } = await query.maybeSingle();
if (error || !data) {
throw new Error('No matching payment request found for certification.');
}
return mapPaymentRequest(data);
}
export async function assertApprovedPaymentForCertificate(
input: PaymentApprovalInput,
authToken?: string | null
): Promise<ApprovedPaymentContext> {
let request = await resolvePaymentRequest(input, authToken);
if (request.status !== 'paid') {
// If pending, attempt self-healing against Cashfree directly
if (request.status === 'pending') {
const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(request.id);
if (isUuid) {
try {
const isPaid = await isOrderPaidOnCashfree(request.id);
if (isPaid) {
logger.info(`[assertApprovedPaymentForCertificate] Reconciled Cashfree order ${request.id} as PAID.`);
const updated = await updatePaymentRequestStatus(request.id, 'paid', null);
request = updated;
}
} catch (pollError) {
logger.warn('[assertApprovedPaymentForCertificate] Cashfree reconciliation check failed:', pollError);
}
}
}
}
if (request.status !== 'paid') {
throw new Error('Payment is not approved yet. Please wait for payment confirmation.');
}
if (!request.certificateIssued) {
const existing = await findCertificateByPaymentRequestId(request.id, authToken);
if (existing) {
return {
request,
alreadyIssuedCertificate: existing,
};
}
return {
request,
alreadyIssuedCertificate: null,
};
}
const issued = await findCertificateByPaymentRequestId(request.id, authToken);
if (!issued) {
throw new Error('Payment request is marked as issued, but certificate record is missing.');
}
return {
request,
alreadyIssuedCertificate: issued,
};
}
export async function markPaymentRequestCertificateIssued(requestId: string, authToken?: string | null): Promise<boolean> {
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
const supabase = serviceRoleKey ? getSupabaseAdmin() : getSupabaseClientForRequest(authToken);
const { error } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.update({
certificate_issued: true,
updated_at: new Date().toISOString(),
})
.eq('id', requestId);
if (error) {
logger.warn(`Failed to mark payment request as certificate issued for ${requestId}:`, error);
return false;
}
return true;
}
export async function reviewPaymentRequest(input: {
paymentRequestId?: string;
txnId?: string;
status: PaymentReviewStatus;
reviewedBy?: string;
reviewNotes?: string;
}): Promise<PaymentRequest> {
const existing = await findPaymentRequestForReview({
paymentRequestId: input.paymentRequestId,
txnId: input.txnId,
authToken: null,
});
if (existing.certificate_issued && input.status === 'rejected') {
throw new Error('Cannot reject a payment request after certificate issuance.');
}
const supabase = getSupabaseAdmin();
const now = new Date().toISOString();
const nextValues: Record<string, unknown> = {
status: input.status,
reviewed_at: now,
review_notes: input.reviewNotes?.trim() || null,
updated_at: now,
};
if (input.reviewedBy?.trim()) {
nextValues.reviewed_by = input.reviewedBy.trim();
}
if (input.status === 'rejected') {
nextValues.certificate_issued = false;
}
const { data, error } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.update(nextValues)
.eq('id', existing.id)
.select('*')
.single();
if (error || !data) {
throw new Error(`Failed to review payment request: ${error?.message || 'unknown error'}`);
}
return mapPaymentRequest(data);
}
export async function updatePaymentRequestStatus(
requestId: string,
status: PaymentRequestStatus,
authToken?: string | null
): Promise<PaymentRequest> {
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
const supabase = serviceRoleKey ? getSupabaseAdmin() : getSupabaseClientForRequest(authToken);
const { data, error } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.update({
status,
updated_at: new Date().toISOString(),
})
.eq('id', requestId)
.select('*')
.single();
if (error || !data) {
const isRlsIssue = !serviceRoleKey;
const rlsHint = isRlsIssue
? ' (This usually happens because SUPABASE_SERVICE_ROLE_KEY is missing in your .env and RLS blocks regular users from updating payment requests)'
: '';
throw new Error(`Failed to update payment request status: ${error?.message || 'unknown error'}${rlsHint}`);
}
if (status === 'paid') {
// Automatically issue certificate and set my_courses progress to 100%
void processApprovedPaymentAndIssueCertificate(requestId).catch((err) => {
logger.error('[Auto-Issuance Trigger Failed in updatePaymentRequestStatus]', err);
});
}
return mapPaymentRequest(data);
}
// Concurrency mutex to prevent duplicate simultaneous issuances for the same payment request
const inFlightIssuancesByOrderId = new Map<string, Promise<CertificatePayload | null>>();
export async function syncCourseProgressToCertified(
userId: string,
courseName: string,
track: string,
issuedAt: string,
score: number = 100
): Promise<void> {
const supabase = getSupabaseAdmin();
const nowIso = issuedAt || new Date().toISOString();
try {
// 1. Upsert into my_courses
const { error: myCourseErr } = await supabase
.from('my_courses')
.upsert({
user_id: userId,
course_name: courseName,
status: 'certified',
current_step: 'certified',
progress_percentage: 100,
assessment_score: score,
certification_track: track,
completed_at: nowIso,
last_activity: nowIso,
updated_at: nowIso,
}, { onConflict: 'user_id,course_name' });
if (myCourseErr) {
logger.warn('[Auto-Issuance] my_courses upsert error:', myCourseErr);
}
// 2. Update course_sessions
await supabase
.from('course_sessions')
.update({
current_step: 'certified',
progress_percentage: 100,
updated_at: nowIso,
})
.eq('user_id', userId)
.eq('course_name', courseName);
} catch (syncErr) {
logger.warn(`[Auto-Issuance] Could not sync course progress to certified for ${courseName}:`, syncErr);
}
}
export async function processApprovedPaymentAndIssueCertificate(
paymentRequestId: string
): Promise<CertificatePayload | null> {
const existingPromise = inFlightIssuancesByOrderId.get(paymentRequestId);
if (existingPromise) {
return existingPromise;
}
const executionPromise = (async () => {
const supabase = getSupabaseAdmin();
// 1. Fetch the payment request
const { data: request, error: reqErr } = await supabase
.from(PAYMENT_REQUESTS_TABLE)
.select('*')
.eq('id', paymentRequestId)
.maybeSingle();
if (reqErr || !request) {
logger.warn(`[Auto-Issuance] Payment request not found for ID: ${paymentRequestId}`, reqErr);
return null;
}
if (request.status !== 'paid') {
logger.warn(`[Auto-Issuance] Payment request ${paymentRequestId} is not marked paid (status: ${request.status})`);
return null;
}
const track = (request.track || 'quickcert') as CertificationTrack;
// 2. Check if certificate is already issued
const existingCert = await findCertificateByPaymentRequestId(request.id, null);
if (existingCert) {
if (!request.certificate_issued) {
await markPaymentRequestCertificateIssued(request.id, null);
}
await syncCourseProgressToCertified(request.user_id, request.skill, track, existingCert.issuedAt);
return existingCert;
}
// 3. Resolve student name & email safely from public.users & Supabase auth
let studentName = '';
let studentEmail = '';
try {
const { data: dbUser } = await supabase
.from('users')
.select('full_name, username, email')
.eq('id', request.user_id)
.maybeSingle();
const { data: authUserData } = await supabase.auth.admin.getUserById(request.user_id);
studentEmail = dbUser?.email || authUserData?.user?.email || '';
studentName =
dbUser?.full_name ||
dbUser?.username ||
authUserData?.user?.user_metadata?.full_name ||
authUserData?.user?.user_metadata?.name ||
(studentEmail ? studentEmail.split('@')[0] : '') ||
'GetCert Student';
} catch (userErr) {
logger.warn(`[Auto-Issuance] Failed to resolve user info for ${request.user_id}:`, userErr);
studentName = 'GetCert Student';
}
// 4. Resolve assessment score from course_sessions
let score = 90;
try {
const { data: sessionData } = await supabase
.from('course_sessions')
.select('flow_state')
.eq('user_id', request.user_id)
.eq('course_name', request.skill)
.order('updated_at', { ascending: false })
.limit(1)
.maybeSingle();
if (sessionData?.flow_state?.assessmentScore) {
score = sessionData.flow_state.assessmentScore;
}
} catch {
// Default to 90
}
// 5. Issue official certificate
const issuedCertificate = await issueCertificate({
userId: request.user_id,
name: studentName,
email: studentEmail,
skill: request.skill,
projectTitle: request.project_title || `${request.skill} Quick Certification`,
githubUrl: '',
proofUrl: '',
finalScore: score,
paymentRequestId: request.id,
paymentTxnId: request.txn_id,
track,
authToken: null,
});
// 6. Mark payment request as certificate issued
await markPaymentRequestCertificateIssued(request.id, null);
// 7. Update my_courses and course_sessions to 100% and certified
await syncCourseProgressToCertified(request.user_id, request.skill, track, issuedCertificate.issuedAt, score);
// 8. Send notification email asynchronously
if (studentEmail) {
sendCertificateIssuedEmail({
to: studentEmail,
name: studentName,
certificate: issuedCertificate,
}).catch((emailErr) => {
logger.warn('[Auto-Issuance] Email dispatch notice:', emailErr);
});
}
logger.info(`[Auto-Issuance] Successfully issued certificate ${issuedCertificate.certificateId} and set my_courses progress to 100% for user ${request.user_id}`);
return issuedCertificate;
})();
inFlightIssuancesByOrderId.set(paymentRequestId, executionPromise);
try {
return await executionPromise;
} finally {
inFlightIssuancesByOrderId.delete(paymentRequestId);
}
}
|