handling changes to stripe account where owner needs to provide information
This commit is contained in:
@@ -2,6 +2,8 @@ const express = require("express");
|
||||
const { authenticateToken, requireVerifiedEmail } = require("../middleware/auth");
|
||||
const { User, Item } = require("../models");
|
||||
const StripeService = require("../services/stripeService");
|
||||
const StripeWebhookService = require("../services/stripeWebhookService");
|
||||
const emailServices = require("../services/email");
|
||||
const logger = require("../utils/logger");
|
||||
const router = express.Router();
|
||||
|
||||
@@ -168,7 +170,7 @@ router.post("/account-sessions", authenticateToken, requireVerifiedEmail, async
|
||||
}
|
||||
});
|
||||
|
||||
// Get account status
|
||||
// Get account status with reconciliation
|
||||
router.get("/account-status", authenticateToken, async (req, res, next) => {
|
||||
let user = null;
|
||||
try {
|
||||
@@ -190,6 +192,64 @@ router.get("/account-status", authenticateToken, async (req, res, next) => {
|
||||
payoutsEnabled: accountStatus.payouts_enabled,
|
||||
});
|
||||
|
||||
// Reconciliation: Compare fetched status with stored User fields
|
||||
const previousPayoutsEnabled = user.stripePayoutsEnabled;
|
||||
const currentPayoutsEnabled = accountStatus.payouts_enabled;
|
||||
const requirements = accountStatus.requirements || {};
|
||||
|
||||
// Check if status has changed and needs updating
|
||||
const statusChanged =
|
||||
previousPayoutsEnabled !== currentPayoutsEnabled ||
|
||||
JSON.stringify(user.stripeRequirementsCurrentlyDue || []) !==
|
||||
JSON.stringify(requirements.currently_due || []);
|
||||
|
||||
if (statusChanged) {
|
||||
reqLogger.info("Reconciling account status from API call", {
|
||||
userId: req.user.id,
|
||||
previousPayoutsEnabled,
|
||||
currentPayoutsEnabled,
|
||||
previousCurrentlyDue: user.stripeRequirementsCurrentlyDue?.length || 0,
|
||||
newCurrentlyDue: requirements.currently_due?.length || 0,
|
||||
});
|
||||
|
||||
// Update user with current status
|
||||
await user.update({
|
||||
stripePayoutsEnabled: currentPayoutsEnabled,
|
||||
stripeRequirementsCurrentlyDue: requirements.currently_due || [],
|
||||
stripeRequirementsPastDue: requirements.past_due || [],
|
||||
stripeDisabledReason: requirements.disabled_reason || null,
|
||||
stripeRequirementsLastUpdated: new Date(),
|
||||
});
|
||||
|
||||
// If payouts just became disabled (true -> false), send notification
|
||||
if (!currentPayoutsEnabled && previousPayoutsEnabled) {
|
||||
reqLogger.warn("Payouts disabled detected during reconciliation", {
|
||||
userId: req.user.id,
|
||||
disabledReason: requirements.disabled_reason,
|
||||
});
|
||||
|
||||
try {
|
||||
const disabledReason = StripeWebhookService.formatDisabledReason(
|
||||
requirements.disabled_reason
|
||||
);
|
||||
|
||||
await emailServices.payment.sendPayoutsDisabledEmail(user.email, {
|
||||
ownerName: user.firstName || user.name,
|
||||
disabledReason,
|
||||
});
|
||||
|
||||
reqLogger.info("Sent payouts disabled email during reconciliation", {
|
||||
userId: req.user.id,
|
||||
});
|
||||
} catch (emailError) {
|
||||
reqLogger.error("Failed to send payouts disabled email", {
|
||||
userId: req.user.id,
|
||||
error: emailError.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
accountId: accountStatus.id,
|
||||
detailsSubmitted: accountStatus.details_submitted,
|
||||
|
||||
Reference in New Issue
Block a user