sss

mail@pastecode.io avatar
unknown
swift
7 months ago
2.5 kB
1
Indexable
Never
private func makeRegistrationDriver(errorTracker: ErrorTracker,
                                     onRegisterTap: Driver<Void>,
                                     activityIndicator: ActivityIndicator,
                                     email: Driver<String?>,
                                     requiredConsents: Driver<RequiredConsents>,
                                     allConsents: Driver<PricelessSpecialsConsents>) -> Driver<Void> {
    let allowedToRegister = makeAllowedToRegisterDriver(onRegisterTap: onRegisterTap,
                                                        email: email,
                                                        requiredConsents: requiredConsents)

    let allowedKeys = allowedToRegister
        .flatMapLatest {
            self.pricelessSpecialsService.getPricelessSpecialsAllowedKeys()
                .trackActivity(activityIndicator)
                .asDriver(trackError: errorTracker)
        }

    let confirmationParams = Driver
        .combineLatest(allConsents, allowedKeys)
        .map { allConsents, allowedKeys in
            let keyTypes = allowedKeys.compactMap { SecKeyType(rawValue: $0) }
            return ConfirmationParams(signingFields: [allConsents.regulations.description,
                                                      allConsents.personalData.description,
                                                      allConsents.email.description,
                                                      allConsents.sms.description],
                                      allowedKeys: keyTypes)
        }

    let registration = confirmationParams
        .withLatestFrom(allConsents)
        .performAuthorizedRequest(router: navigator.router, errorTracker: errorTracker) { confirmationParams, allConsents in
            let body = RegisterForPricelessSpecialsBody(signature: confirmationParams.authorizedRequestBody().signature,
                                                        signingKeyType: confirmationParams.signingKeyType,
                                                        consents: allConsents)
            return self.pricelessSpecialsService.registerForPricelessSpecials(with: body)
        }
        .do(onNext: { response in
            guard let cards = response?.registerCards else {
                self.navigator.navigate(to: .unknownError)
                return
            }
            self.navigateToRegistrationOutcome(cards: cards)
        })
        .asVoid()

    return registration
}