in lib/holiday-stops/src/main/scala/com/gu/salesforce/holiday_stops/SalesforceHolidayStopRequest.scala [285:348]
def buildBody(
holidayStopRequestId: HolidayStopRequestId,
startDate: LocalDate,
endDate: LocalDate,
issuesData: List[IssueData],
existingPublicationsThatWereToBeStopped: List[HolidayStopRequestsDetail],
): Either[String, CompositeRequest] = {
val masterRecordToBePatched = CompositePart(
method = "PATCH",
url = s"$holidayStopRequestSfObjectsBaseUrl/${holidayStopRequestId.value}",
referenceId = holidayStopRequestSfObjectRef, // constant since only one of these in the request
body = Json.toJson(
AmendHolidayStopRequestItselfBody(
Start_Date__c = HolidayStopRequestStartDate(startDate),
End_Date__c = HolidayStopRequestEndDate(endDate),
),
)(Json.writes[AmendHolidayStopRequestItselfBody]),
)
val detailRecordsToBeAdded = issuesData
.filterNot(issueData =>
existingPublicationsThatWereToBeStopped.exists(_.Stopped_Publication_Date__c.value == issueData.issueDate),
)
.map { issueData =>
CompositePart(
method = "POST",
url = s"$sfObjectsBaseUrl$HolidayStopRequestsDetailSfObjectRef",
referenceId = s"CREATE_DETAIL_${generateId()}",
body = Json.toJson(
AddHolidayStopRequestDetailBody(
Holiday_Stop_Request__c = holidayStopRequestId,
Stopped_Publication_Date__c = issueData.issueDate,
Estimated_Price__c = Price(issueData.credit),
Expected_Invoice_Date__c =
HolidayStopRequestsDetailExpectedInvoiceDate(issueData.nextBillingPeriodStartDate),
),
)(Json.writes[AddHolidayStopRequestDetailBody]),
)
}
for {
detailRecordsToBeDeleted <- existingPublicationsThatWereToBeStopped
.filterNot(holidayStopRequestDetail =>
issuesData.map(_.issueDate).contains(holidayStopRequestDetail.Stopped_Publication_Date__c.value),
)
.traverse(holidayStopRequestDetail => {
if (holidayStopRequestDetail.Is_Actioned__c)
Left("actioned publications cannot be deleted")
else
Right(
CompositePart(
method = "DELETE",
url = s"$sfObjectsBaseUrl$HolidayStopRequestsDetailSfObjectRef/${holidayStopRequestDetail.Id.value}",
referenceId = s"DELETE_DETAIL_${generateId()}",
body = JsNull,
),
)
})
} yield CompositeRequest(
allOrNone = true,
compositeRequest = masterRecordToBePatched :: detailRecordsToBeAdded ++ detailRecordsToBeDeleted,
)
}