/// <reference path="../pb_data/types.d.ts" />

onRecordRequestOTPRequest((e) => {
  // create a user with the OTP email if it doesn't exist

  if (!e.record) {
    const email = e.requestInfo().body['email']
    const record = new Record(e.collection)
    record.setEmail(email)
    record.setPassword($security.randomString(30))

    const language = e.requestInfo().headers['language'] || 'en'
    record.set('language', language)

    e.app.save(record)

    e.record = record
  }

  return e.next()
}, 'users')
