fix(launch-flow): auto-link /join signups to existing PreRegistration
When a /join submitter's email matches a pending/selected/invited PreRegistration, mark the pre-reg as accepted and link memberId to the new Member. Prevents the same person from appearing as both an active member and an unaccepted pre-registrant. Silent — no email, no UI. Adds the PreRegistration mock to helcim-customer and free-signup-flow test suites, since both invoke the customer handler at runtime.
This commit is contained in:
parent
d4000c18cf
commit
da5e7efcb7
3 changed files with 33 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ import { getRequestHeader, getRequestIP } from 'h3'
|
|||
import Member from '../../models/member.js'
|
||||
import { connectDB } from '../../utils/mongoose.js'
|
||||
import { createHelcimCustomer } from '../../utils/helcim.js'
|
||||
import PreRegistration from '../../models/preRegistration.js'
|
||||
import { sendMagicLink } from '../../utils/magicLink.js'
|
||||
import { setPaymentBridgeCookie } from '../../utils/auth.js'
|
||||
import { rateLimit } from '../../utils/rateLimit.js'
|
||||
|
|
@ -82,6 +83,32 @@ export default defineEventHandler(async (event) => {
|
|||
})
|
||||
}
|
||||
|
||||
// If this email matches a pending pre-registrant, mark the PreRegistration
|
||||
// as accepted and link it to the new Member. Silent — keeps /join and
|
||||
// /admin/pre-registrants from showing the same person twice.
|
||||
try {
|
||||
const preReg = await PreRegistration.findOne({ email: normalizedEmail })
|
||||
if (
|
||||
preReg &&
|
||||
!preReg.memberId &&
|
||||
['pending', 'selected', 'invited'].includes(preReg.status)
|
||||
) {
|
||||
await PreRegistration.findByIdAndUpdate(
|
||||
preReg._id,
|
||||
{
|
||||
$set: {
|
||||
status: 'accepted',
|
||||
acceptedAt: new Date(),
|
||||
memberId: member._id,
|
||||
},
|
||||
},
|
||||
{ runValidators: false }
|
||||
)
|
||||
}
|
||||
} catch (linkError) {
|
||||
console.error('Failed to link PreRegistration to new member:', linkError)
|
||||
}
|
||||
|
||||
await sendMagicLink(normalizedEmail, {
|
||||
subject: 'Verify your Ghost Guild signup',
|
||||
intro: 'Verify your email to finish your Ghost Guild signup:',
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@ vi.mock('../../../server/models/member.js', () => ({
|
|||
findOneAndUpdate: vi.fn()
|
||||
}
|
||||
}))
|
||||
vi.mock('../../../server/models/preRegistration.js', () => ({
|
||||
default: { findOne: vi.fn().mockResolvedValue(null), findByIdAndUpdate: vi.fn() }
|
||||
}))
|
||||
vi.mock('../../../server/utils/mongoose.js', () => ({ connectDB: vi.fn() }))
|
||||
vi.mock('../../../server/utils/helcim.js', () => ({
|
||||
createHelcimCustomer: vi.fn(),
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ import { createMockEvent } from '../helpers/createMockEvent.js'
|
|||
vi.mock('../../../server/models/member.js', () => ({
|
||||
default: { findOne: vi.fn(), create: vi.fn(), findByIdAndUpdate: vi.fn() }
|
||||
}))
|
||||
vi.mock('../../../server/models/preRegistration.js', () => ({
|
||||
default: { findOne: vi.fn().mockResolvedValue(null), findByIdAndUpdate: vi.fn() }
|
||||
}))
|
||||
vi.mock('../../../server/utils/mongoose.js', () => ({ connectDB: vi.fn() }))
|
||||
vi.mock('../../../server/utils/helcim.js', () => ({
|
||||
createHelcimCustomer: vi.fn()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue