generatePasswordResetOTP method

  1. @override
Future<RequestResponse<GenerateResetOTPResponse?>> generatePasswordResetOTP({
  1. RESET_ID_TYPE? idType,
  2. String? id,
  3. OTP_MEDIUM? medium,
  4. String? mediumId,
})
override

Generates the OTP and sends it through the chosen medium and mediumId.

This is the first step for resetting a forgotten password.

Implementation

@override
Future<RequestResponse<GenerateResetOTPResponse?>> generatePasswordResetOTP({
  RESET_ID_TYPE? idType,
  String? id,
  OTP_MEDIUM? medium,
  String? mediumId,
}) async {
  await getFrappe().checkAppInstalled(features: ['generatePasswordResetOTP']);

  assert(id != null && id.isNotEmpty, "ID can't be empty");
  assert(idType != null, "ID type can't be null");
  assert(mediumId != null && mediumId.isNotEmpty, "Medium ID can't be empty");
  assert(medium != null, "Medium can't be null");

  final idTypeAsString = EnumToString.convertToString(idType);
  final mediumAsString = EnumToString.convertToString(medium);

  final response = await Request.initiateRequest(
      url: config.hostUrl,
      method: HttpMethod.POST,
      contentType: ContentTypeLiterals.APPLICATION_JSON,
      data: <String, dynamic>{
        'cmd': 'renovation_core.utils.forgot_pwd.generate_otp',
        'id_type': idTypeAsString,
        'id': id,
        'medium': mediumAsString,
        'medium_id': mediumId
      });

  if (response.isSuccess) {
    final otpResponse =
        GenerateResetOTPResponse.fromJson(response.data!.message);

    if (otpResponse.sent!) {
      return RequestResponse.success(otpResponse,
          rawResponse: response.rawResponse);
    } else {
      return RequestResponse.fail(
        ErrorDetail(
          title: otpResponse.reason,
          info: Information(httpCode: 400),
        ),
      )..data = otpResponse;
    }
  }
  return RequestResponse.fail(response.error!);
}