Skip to content

Instantly share code, notes, and snippets.

@tdrozdowski
Last active August 29, 2015 13:57
Show Gist options
  • Save tdrozdowski/9899212 to your computer and use it in GitHub Desktop.
Save tdrozdowski/9899212 to your computer and use it in GitHub Desktop.
ReactiveMongo 0.10 with JSONCollection - Upsert
private def buildUsageUpsert(usage : Usage) = {
Json.obj(
"$setOnInsert" ->
Json.obj(
"cpCode" -> usage.customer.cpCode,
"zuora.id" -> usage.customer.zuoraId,
"zuora.subscriptionId" -> usage.customer.subscriptionId,
"month" -> usage.month,
"year" -> usage.year,
"uom" -> "GB"
),
"$set" -> Json.obj(
"monthlyTotal" -> usage.monthlyTotal ,
"completed" -> usage.completed,
"lastUpdated" -> usage.lastUpdated,
"completedWhen" -> usage.completedWhen
)
)
}
private def usageQuery(usage : Usage) = {
Json.obj("cpCode" -> usage.customer.cpCode, "month" -> usage.month, "year" -> usage.year)
}
private def upsertUsage(usage : Usage) = {
val usageUpsert = buildUsageUpsert(usage)
Logger.debug(s"Upserting usage for ${usage.customer.cpCode} with upsert: $usageUpsert")
monthlyUsage.update(usageQuery(usage), usageUpsert, upsert = true).map {
le =>
if (le.inError) {
Logger.error(s"Error updating usage for cpCode ${usage.customer.cpCode} : ${le.errMsg.getOrElse("No details!")}")
false
} else {
Logger.info(s"Successfully updated usage for cpCode ${usage.customer.cpCode}")
true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment