Accept buyer phone in grant API and enrich existing users
payment-router now forwards name/phone from the order. Store phone on create; for existing users fill name (when it is an email placeholder) and phone (when empty) without overwriting meaningful data. Also stop dropping the parsed phone column in CSV import.
This commit is contained in:
@@ -200,6 +200,7 @@ export async function applyImport(
|
||||
data: {
|
||||
name: row.name,
|
||||
email: row.email,
|
||||
phone: row.phone || null,
|
||||
emailVerified: options.autoVerifyEmail,
|
||||
role: "student",
|
||||
},
|
||||
@@ -231,10 +232,16 @@ export async function applyImport(
|
||||
|
||||
created++;
|
||||
} else if (row.status === "update" && row.existingId) {
|
||||
const existing = await prisma.user.findUnique({
|
||||
where: { id: row.existingId },
|
||||
select: { phone: true },
|
||||
});
|
||||
await prisma.user.update({
|
||||
where: { id: row.existingId },
|
||||
data: {
|
||||
name: row.name || undefined,
|
||||
// don't overwrite an already-filled phone
|
||||
phone: existing?.phone ? undefined : row.phone || undefined,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ export async function POST(request: NextRequest) {
|
||||
let body: {
|
||||
email?: string;
|
||||
name?: string;
|
||||
phone?: string;
|
||||
course_slugs?: string[];
|
||||
order_id?: string;
|
||||
};
|
||||
@@ -30,6 +31,7 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const email = (body.email ?? "").trim().toLowerCase();
|
||||
const name = (body.name ?? "").trim();
|
||||
const phone = (body.phone ?? "").trim().slice(0, 30);
|
||||
const slugs = Array.isArray(body.course_slugs)
|
||||
? body.course_slugs.filter((s) => typeof s === "string" && s.trim())
|
||||
: [];
|
||||
@@ -66,7 +68,7 @@ export async function POST(request: NextRequest) {
|
||||
// Find or create user
|
||||
let user = await prisma.user.findFirst({
|
||||
where: { email: { equals: email, mode: "insensitive" } },
|
||||
select: { id: true, name: true },
|
||||
select: { id: true, name: true, email: true, phone: true },
|
||||
});
|
||||
let tempPassword: string | null = null;
|
||||
if (!user) {
|
||||
@@ -76,14 +78,25 @@ export async function POST(request: NextRequest) {
|
||||
data: {
|
||||
email,
|
||||
name: name || email,
|
||||
phone: phone || null,
|
||||
emailVerified: true,
|
||||
mustChangePassword: true,
|
||||
accounts: {
|
||||
create: { accountId: email, providerId: "credential", password: hash },
|
||||
},
|
||||
},
|
||||
select: { id: true, name: true },
|
||||
select: { id: true, name: true, email: true, phone: true },
|
||||
});
|
||||
} else {
|
||||
// Enrich existing user from the order, never overwriting meaningful data:
|
||||
// name only if the current one is empty or an email placeholder, phone only if empty.
|
||||
const enrich: { name?: string; phone?: string } = {};
|
||||
if (name && (!user.name || user.name === user.email)) enrich.name = name;
|
||||
if (phone && !user.phone) enrich.phone = phone;
|
||||
if (Object.keys(enrich).length > 0) {
|
||||
await prisma.user.update({ where: { id: user.id }, data: enrich });
|
||||
if (enrich.name) user.name = enrich.name;
|
||||
}
|
||||
}
|
||||
|
||||
// Enroll + audit log
|
||||
|
||||
Reference in New Issue
Block a user