@@ -23,18 +23,38 @@ import org.mongodb.scala.model.Updates.{combine, set}
2323import org .mongodb .scala .model .{FindOneAndUpdateOptions , IndexModel , IndexOptions , ReturnDocument }
2424import org .mongodb .scala .{MongoClient , MongoCollection }
2525import play .api .Logger
26- import play .api .libs .json .{ Json , OFormat }
26+ import play .api .libs .json ._
2727import uk .gov .hmrc .apiscope .models .Scope
2828import uk .gov .hmrc .mongo .MongoComponent
2929import uk .gov .hmrc .mongo .play .json .{Codecs , CollectionFactory , PlayMongoRepository }
3030
3131import javax .inject .{Inject , Singleton }
3232import scala .collection .Seq
3333import scala .concurrent .{ExecutionContext , Future }
34-
34+ import play .api .libs .json .Reads
35+ import uk .gov .hmrc .apiscope .models .ConfidenceLevel
36+ import play .api .libs .functional .syntax ._
3537
3638private object ScopeFormats {
37- implicit val scopeFormat : OFormat [Scope ] = Json .format[Scope ]
39+ implicit val scopeRead : Reads [Scope ] = (
40+ (JsPath \ " key" ).read[String ] and
41+ (JsPath \ " name" ).read[String ] and
42+ (JsPath \ " description" ).read[String ] and
43+ (JsPath \ " confidenceLevel" ).readNullable[Int ]
44+ .map[Option [ConfidenceLevel ]](_ match {
45+ case None => None
46+ case Some (50 ) => Some (ConfidenceLevel .L50 )
47+ case Some (100 ) => Some (ConfidenceLevel .L200 )
48+ case Some (200 ) => Some (ConfidenceLevel .L200 )
49+ case Some (250 ) => Some (ConfidenceLevel .L250 )
50+ case Some (300 ) => Some (ConfidenceLevel .L200 )
51+ case Some (500 ) => Some (ConfidenceLevel .L500 )
52+ case Some (i) => throw new RuntimeException (s " Bad data in confidence level of $i" )
53+ })
54+ )(Scope .apply _)
55+
56+ implicit val scopeWrites : OWrites [Scope ] = Json .writes[Scope ]
57+ implicit val scopeFormat : OFormat [Scope ] = OFormat (scopeRead,scopeWrites)
3858}
3959
4060@ Singleton
@@ -48,22 +68,11 @@ private object ScopeFormats {
4868 .name(" keyIndex" )
4969 .background(true )
5070 .unique(true ))),
51- replaceIndexes = true
71+ replaceIndexes = true ,
72+ extraCodecs = Seq (Codecs .playFormatCodec(ScopeFormats .scopeFormat))
5273 ) {
5374 private val logger = Logger (this .getClass)
5475
55- override lazy val collection : MongoCollection [Scope ] =
56- CollectionFactory
57- .collection(mongoComponent.database, collectionName, domainFormat)
58- .withCodecRegistry(
59- fromRegistries(
60- fromCodecs(
61- Codecs .playFormatCodec(domainFormat)
62- ),
63- MongoClient .DEFAULT_CODEC_REGISTRY
64- )
65- )
66-
6776 def save (scope : Scope ) : Future [Scope ] = {
6877 var updateSeq = Seq (
6978 set(" key" , Codecs .toBson(scope.key)),
0 commit comments