Skip to content

Commit

Permalink
feat #728: modify the type of validUntil
Browse files Browse the repository at this point in the history
  • Loading branch information
helakaraa committed Sep 16, 2024
1 parent 0b12f59 commit 3dcfb9a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion daikoku/app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ class ApiController(
customMaxPerMonth = (body \ "customMaxPerMonth").asOpt[Long],
customReadOnly = (body \ "customReadOnly").asOpt[Boolean],
adminCustomName = (body \ "adminCustomName").asOpt[String],
validUntil = (body \ "validUntil").asOpt[String],
validUntil = (body \ "validUntil").asOpt(DateTimeFormat),
)
result <-
EitherT(apiService.updateSubscription(ctx.tenant, subToSave, plan))
Expand Down
3 changes: 2 additions & 1 deletion daikoku/app/controllers/OtoroshiSettingsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ class OtoroshiSettingsController(
.flatMap(_.asOpt[Map[String, String]])
.getOrElse(Map.empty[String, String]),
rotation = None,
readOnly = readOnlyOpt.getOrElse(false)
readOnly = readOnlyOpt.getOrElse(false),
validUntil = None,
)
}

Expand Down
6 changes: 2 additions & 4 deletions daikoku/app/domain/SchemaDefinition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2109,9 +2109,7 @@ object SchemaDefinition {
)
),
Field("createdAt", DateTimeUnitype, resolve = _.value.createdAt),
Field("validUntil",
OptionType(StringType),
resolve = _.value.validUntil),
Field("validUntil", OptionType(DateTimeUnitype), resolve = _.value.validUntil),
Field(
"team",
OptionType(TeamObjectType),
Expand Down Expand Up @@ -3569,7 +3567,7 @@ object SchemaDefinition {
),
ReplaceField(
"validUntil",
Field("validUntil", DateTimeUnitype, resolve = _.value.validUntil)
Field("validUntil", OptionType(DateTimeUnitype), resolve = _.value.validUntil)
)
)
lazy val TranslationType =
Expand Down
13 changes: 9 additions & 4 deletions daikoku/app/domain/apikeyEntities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ case class ApikeyCustomization(
metadata: JsObject = play.api.libs.json.Json.obj(),
customMetadata: Seq[CustomMetadata] = Seq.empty,
tags: JsArray = play.api.libs.json.Json.arr(),
restrictions: ApiKeyRestrictions = ApiKeyRestrictions()
restrictions: ApiKeyRestrictions = ApiKeyRestrictions(),
validUntil: Option[DateTime] = None,
) extends CanJson[ApikeyCustomization] {
def asJson: JsValue = json.ApikeyCustomizationFormat.writes(this)
}
Expand Down Expand Up @@ -59,7 +60,7 @@ case class ApiSubscription(
apiKey: OtoroshiApiKey, // TODO: add the actual plan at the time of the subscription
plan: UsagePlanId,
createdAt: DateTime,
validUntil: Option[String] = None,
validUntil: Option[DateTime] = None,
team: TeamId,
api: ApiId,
by: UserId,
Expand Down Expand Up @@ -109,7 +110,10 @@ case class ApiSubscription(
"team" -> json.TeamIdFormat.writes(team),
"api" -> json.ApiIdFormat.writes(api),
"createdAt" -> json.DateTimeFormat.writes(createdAt),
"validUntil" -> validUntil.map(JsString).getOrElse(JsNull).as[JsValue],
"validUntil" -> validUntil
.map(json.DateTimeFormat.writes)
.getOrElse(JsNull)
.as[JsValue],
"customName" -> customName
.map(id => JsString(id))
.getOrElse(JsNull)
Expand Down Expand Up @@ -137,7 +141,8 @@ case class ActualOtoroshiApiKey(
tags: Set[String] = Set.empty[String],
metadata: Map[String, String] = Map.empty[String, String],
restrictions: ApiKeyRestrictions = ApiKeyRestrictions(),
rotation: Option[ApiKeyRotation]
rotation: Option[ApiKeyRotation],
validUntil : Option[Long] = None
) extends CanJson[OtoroshiApiKey] {
override def asJson: JsValue = json.ActualOtoroshiApiKeyFormat.writes(this)
def asOtoroshiApiKey: OtoroshiApiKey =
Expand Down
18 changes: 12 additions & 6 deletions daikoku/app/domain/json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,8 @@ object json {
.asOpt(SeqCustomMetadataFormat)
.getOrElse(Seq.empty),
tags = (json \ "tags").asOpt[JsArray].getOrElse(Json.arr()),
restrictions = (json \ "restrictions").as(ApiKeyRestrictionsFormat)
restrictions = (json \ "restrictions").as(ApiKeyRestrictionsFormat),
validUntil = (json \ "validUntil").asOpt(DateTimeFormat)
)
)
} recover {
Expand All @@ -1679,7 +1680,9 @@ object json {
o.customMetadata.map(CustomMetadataFormat.writes)
),
"tags" -> o.tags,
"restrictions" -> o.restrictions.asJson
"restrictions" -> o.restrictions.asJson,
"validUntil" -> o.validUntil.map(DateTimeFormat.writes)
.getOrElse(JsNull).as[JsValue]
)
}
val ApiKeyRestrictionsFormat = new Format[ApiKeyRestrictions] {
Expand Down Expand Up @@ -2731,7 +2734,7 @@ object json {
team = (json \ "team").as(TeamIdFormat),
api = (json \ "api").as(ApiIdFormat),
createdAt = (json \ "createdAt").as(DateTimeFormat),
validUntil = (json \ "validUntil").asOpt[String],
validUntil = (json \ "validUntil").asOpt(DateTimeFormat),
by = (json \ "by").as(UserIdFormat),
customName = (json \ "customName").asOpt[String],
adminCustomName = (json \ "adminCustomName").asOpt[String],
Expand Down Expand Up @@ -2781,7 +2784,7 @@ object json {
"api" -> ApiIdFormat.writes(o.api),
"createdAt" -> DateTimeFormat.writes(o.createdAt),
"validUntil"-> o.validUntil
.map(id => JsString(id))
.map(DateTimeFormat.writes)
.getOrElse(JsNull)
.as[JsValue],
"by" -> UserIdFormat.writes(o.by),
Expand Down Expand Up @@ -3130,7 +3133,9 @@ object json {
"rotation" -> apk.rotation
.map(ApiKeyRotationFormat.writes)
.getOrElse(JsNull)
.as[JsValue]
.as[JsValue],
"validUntil" -> apk.validUntil

)

override def reads(json: JsValue): JsResult[ActualOtoroshiApiKey] =
Expand Down Expand Up @@ -3165,7 +3170,8 @@ object json {
.asOpt[Set[String]]
.getOrElse(Set.empty[String]),
restrictions = (json \ "restrictions").as(ApiKeyRestrictionsFormat),
rotation = (json \ "rotation").asOpt(ApiKeyRotationFormat)
rotation = (json \ "rotation").asOpt(ApiKeyRotationFormat),
validUntil = (json \ "validUntil").asOpt[Long]
)
} map {
case sd => JsSuccess(sd)
Expand Down
7 changes: 5 additions & 2 deletions daikoku/app/utils/ApiService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ class ApiService(
"daikoku__tags" -> processedTags.mkString(" | ")
) ++ processedMetadata,
rotation =
plan.autoRotation.map(enabled => ApiKeyRotation(enabled = enabled))
plan.autoRotation.map(enabled => ApiKeyRotation(enabled = enabled)),
validUntil = plan.otoroshiTarget.flatMap(_.apikeyCustomization
.validUntil.map(_.getMillis))
)

plan match {
Expand Down Expand Up @@ -761,7 +763,8 @@ class ApiService(
metadata = apiKey.metadata ++ subscription.customMetadata
.flatMap(_.asOpt[Map[String, String]])
.getOrElse(Map.empty[String, String]),
readOnly = subscription.customReadOnly.getOrElse(apiKey.readOnly)
readOnly = subscription.customReadOnly.getOrElse(apiKey.readOnly),
validUntil = subscription.validUntil.map(_.getMillis)
)
)(otoSettings)
)
Expand Down
6 changes: 4 additions & 2 deletions daikoku/javascript/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ interface IOtoroshiTarget {
forbidden: Array<IPath>;
notFound: Array<IPath>;
};
validUntil? : string;
};
}

Expand Down Expand Up @@ -391,7 +392,7 @@ export interface IBaseSubscription {
customMaxPerDay?: number;
customReadOnly?: boolean;
adminCustomName?: string;
parent: string | null;
parent? : object;
parentUp: boolean;
}

Expand Down Expand Up @@ -444,9 +445,10 @@ export interface ISubscriptionCustomization {
customReadOnly?: boolean;
adminCustomName?: string;
validUntil?: string;
parent : string | null;
parent? : object;
}


export interface ISubscriptionExtended extends ISubscription {
parentUp: boolean;
planType: string;
Expand Down
3 changes: 2 additions & 1 deletion daikoku/test/daikoku/ConsumptionControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ class ConsumptionControllerSpec()
tags = Set.empty[String],
restrictions = ApiKeyRestrictions(),
metadata = Map(),
rotation = None
rotation = None,
validUntil = payperUserSub.validUntil.map(_.getMillis)
)

wireMockServer.isRunning mustBe true
Expand Down

0 comments on commit 3dcfb9a

Please sign in to comment.