Repository: corona-warn-app/cwa-app-android
File: Corona-Warn-App/src/main/java/de/rki/coronawarnapp/familytest/core/repository/CoronaTestProcessor.kt (Kotlin)

Code before:
    suspend fun pollServer(familyTest: FamilyCoronaTest): PollResult = try {
        val response = try {
            coronaTestService.checkTestResult(familyTest.registrationToken)
        } catch (e: BadRequestException) {
            if (familyTest.isOlderThan21Days(timeStamper.nowUTC)) {
                Timber.v("HTTP 400 error after 21 days, remapping to PCR_OR_RAT_REDEEMED.")
                CoronaTestResultResponse(coronaTestResult = PCR_OR_RAT_REDEEMED)
            } else {
                throw e
            }
        }

        val testResult = when (familyTest.type) {
            RAPID_ANTIGEN -> response.coronaTestResult.toValidatedRaResult()
            PCR -> response.coronaTestResult.toValidatedPcrResult()
        }

        val update = CoronaTestUpdate(
            coronaTestResult = testResult,
            sampleCollectedAt = response.sampleCollectedAt,
            labId = response.labId,
        )

        PollResult.Success(
            original = familyTest,
            updated = familyTest.updateFromResponse(update)
        )
    } catch (e: Exception) {
        Timber.e(e, "Failed to poll server for  %s", familyTest)
        if (e !is CwaWebException) e.report(ExceptionCategory.INTERNAL)
        PollResult.Error(
            identifier = familyTest.identifier,
            cause = e
        )
    }

    private fun createServerRequest(qrCode: CoronaTestQRCode): RegistrationRequest {
        return when (qrCode) {
            is CoronaTestQRCode.PCR -> {
                val dateOfBirth = qrCode.dateOfBirth
                val dateOfBirthKey = if (qrCode.isDccConsentGiven && dateOfBirth != null) {
                    DateOfBirthKey(qrCode.qrCodeGUID, dateOfBirth)
                } else null

                RegistrationRequest(
                    key = qrCode.qrCodeGUID,
                    dateOfBirthKey = dateOfBirthKey,
                    type = VerificationKeyType.GUID,
                )
            }


Review comment (suggestion):
This will overwrite other updates that have been done simultaneously. I have done it with an update function within a transaction on purpose. We are overwriting all fields that may have been changed from elsewhere in the meantime between reading from database and the end of the backend call.

Code after:
            val response = try {
                coronaTestService.checkTestResult(test.registrationToken)
            } catch (e: BadRequestException) {
                if (test.isOlderThan21Days(timeStamper.nowUTC)) {
                    Timber.v("HTTP 400 error after 21 days, remapping to PCR_OR_RAT_REDEEMED.")
                    CoronaTestResultResponse(coronaTestResult = PCR_OR_RAT_REDEEMED)
                } else {
                    throw e
                }
            }

            val testResult = when (test.type) {
                RAPID_ANTIGEN -> response.coronaTestResult.toValidatedRaResult()
                PCR -> response.coronaTestResult.toValidatedPcrResult()
            }

            ServerResponse.CoronaTestResultUpdate(
                coronaTestResult = testResult,
                sampleCollectedAt = response.sampleCollectedAt,
                labId = response.labId,
            )
        } catch (e: Exception) {
            Timber.e(e, "Failed to poll server for  %s", test)
            if (e !is CwaWebException) e.report(ExceptionCategory.INTERNAL)
            return ServerResponse.Error(e)
        }
    }

    private fun createServerRequest(qrCode: CoronaTestQRCode): RegistrationRequest {
        return when (qrCode) {
            is CoronaTestQRCode.PCR -> {
                val dateOfBirth = qrCode.dateOfBirth
                val dateOfBirthKey = if (qrCode.isDccConsentGiven && dateOfBirth != null) {
                    DateOfBirthKey(qrCode.qrCodeGUID, dateOfBirth)
                } else null

                RegistrationRequest(
                    key = qrCode.qrCodeGUID,
                    dateOfBirthKey = dateOfBirthKey,
                    type = VerificationKeyType.GUID,
                )
            }

            is CoronaTestQRCode.RapidPCR -> RegistrationRequest(
                key = qrCode.registrationIdentifier,
                dateOfBirthKey = null,
                type = VerificationKeyType.GUID
            )

            is CoronaTestQRCode.RapidAntigen -> RegistrationRequest(
                key = qrCode.registrationIdentifier,