Skip to content

Commit

Permalink
FIX #780
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Dec 5, 2024
1 parent 0f2c5c0 commit 76e7d85
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 117 deletions.
7 changes: 5 additions & 2 deletions daikoku/app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,8 @@ class ApiController(
customReadOnly = customReadOnly,
adminCustomName = adminCustomName,
motivation = motivation,
parentSubscriptionId = Some(ApiSubscriptionId(apiKeyId))
parentSubscriptionId = Some(ApiSubscriptionId(apiKeyId)),
customName = None
)
}
}
Expand All @@ -1267,6 +1268,7 @@ class ApiController(
implicit val language: String = ctx.request.getLanguage(ctx.tenant)
implicit val currentUser: User = ctx.user

val customName = ctx.request.body.getBodyField[String]("customName")
val motivation = ctx.request.body.getBodyField[JsObject]("motivation")
val customMaxPerSecond =
ctx.request.body.getBodyField[Long]("customMaxPerSecond")
Expand All @@ -1292,7 +1294,8 @@ class ApiController(
customMaxPerMonth = customMaxPerMonth,
customReadOnly = customReadOnly,
adminCustomName = adminCustomName,
motivation = motivation
motivation = motivation,
customName = customName
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion daikoku/app/domain/apikeyEntities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ case class SubscriptionDemand(
customMaxPerSecond: Option[Long] = None,
customMaxPerDay: Option[Long] = None,
customMaxPerMonth: Option[Long] = None,
adminCustomName: Option[String] = None
adminCustomName: Option[String] = None,
customName: Option[String] = None
) extends CanJson[SubscriptionDemand] {
override def asJson: JsValue = json.SubscriptionDemandFormat.writes(this)
}
Expand Down
7 changes: 6 additions & 1 deletion daikoku/app/domain/json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2918,6 +2918,10 @@ object json {
.getOrElse(JsNull)
.as[JsValue],
"adminCustomName" -> o.adminCustomName
.map(JsString.apply)
.getOrElse(JsNull)
.as[JsValue],
"customName" -> o.customName
.map(JsString.apply)
.getOrElse(JsNull)
.as[JsValue]
Expand Down Expand Up @@ -2948,7 +2952,8 @@ object json {
customMaxPerDay = (json \ "customMaxPerDay").asOpt[Long],
customMaxPerMonth = (json \ "customMaxPerMonth").asOpt[Long],
customReadOnly = (json \ "customReadOnly").asOpt[Boolean],
adminCustomName = (json \ "adminCustomName").asOpt[String]
adminCustomName = (json \ "adminCustomName").asOpt[String],
customName = (json \ "customName").asOpt[String]
)
)
} recover {
Expand Down
23 changes: 15 additions & 8 deletions daikoku/app/utils/ApiService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ class ApiService(
adminCustomName: Option[String] = None,
thirdPartySubscriptionInformations: Option[
ThirdPartySubscriptionInformations
]
],
customName: Option[String]
): Future[Either[AppError, ApiSubscription]] = {
def createKey(
api: Api,
Expand Down Expand Up @@ -262,7 +263,7 @@ class ApiService(
team = team.id,
api = api.id,
by = user.id,
customName = None,
customName = customName,
rotation = plan.autoRotation.map(rotation =>
ApiSubscriptionRotation(enabled = rotation)
),
Expand Down Expand Up @@ -2163,7 +2164,8 @@ class ApiService(
customReadOnly = demand.customReadOnly,
adminCustomName = demand.adminCustomName,
thirdPartySubscriptionInformations =
maybeSubscriptionInformations
maybeSubscriptionInformations,
customName = demand.customName
)
)
administrators <- EitherT.liftF(
Expand Down Expand Up @@ -2253,7 +2255,8 @@ class ApiService(
customMaxPerDay: Option[Long],
customMaxPerMonth: Option[Long],
customReadOnly: Option[Boolean],
adminCustomName: Option[String]
adminCustomName: Option[String],
customName: Option[String]
)(implicit language: String, currentUser: User) = {
import cats.implicits._

Expand Down Expand Up @@ -2411,7 +2414,8 @@ class ApiService(
customMaxPerDay,
customMaxPerMonth,
customReadOnly,
adminCustomName
adminCustomName,
customName
)
} yield result

Expand All @@ -2431,7 +2435,8 @@ class ApiService(
customMaxPerDay: Option[Long],
customMaxPerMonth: Option[Long],
customReadOnly: Option[Boolean],
adminCustomName: Option[String]
adminCustomName: Option[String],
customName: Option[String]
)(implicit language: String): EitherT[Future, AppError, Result] = {
import cats.implicits._

Expand All @@ -2457,7 +2462,8 @@ class ApiService(
plan,
team,
apiKeyId,
thirdPartySubscriptionInformations = None
thirdPartySubscriptionInformations = None,
customName = customName
)
).map(s =>
Ok(
Expand Down Expand Up @@ -2493,7 +2499,8 @@ class ApiService(
customMaxPerDay = customMaxPerDay,
customMaxPerMonth = customMaxPerMonth,
customReadOnly = customReadOnly,
adminCustomName = adminCustomName
adminCustomName = adminCustomName,
customName = customName
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ export const TeamApiKeysForApi = () => {
};

const toggleApiKey = (subscription: ISubscription) => {
console.debug("toggle")
return Services.archiveApiKey(
currentTeam._id,
subscription._id,
Expand Down
5 changes: 3 additions & 2 deletions daikoku/javascript/src/components/frontend/api/ApiHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,15 @@ export const ApiHome = ({
);
};

const askForApikeys = ({ team, plan, apiKey, motivation }: { team: string, plan: IUsagePlan, apiKey?: ISubscription, motivation?: object }) => {
const askForApikeys = ({ team, plan, apiKey, motivation, customName }:
{ team: string, plan: IUsagePlan, apiKey?: ISubscription, motivation?: object, customName?: string }) => {
const planName = formatPlanType(plan, translate);

if (api) {
return (
apiKey
? Services.extendApiKey(api._id, apiKey._id, team, plan._id, motivation)
: Services.askForApiKey(api._id, team, plan._id, motivation)
: Services.askForApiKey(api._id, team, plan._id, motivation, customName)
).then((result) => {

if (isError(result)) {
Expand Down
131 changes: 76 additions & 55 deletions daikoku/javascript/src/components/frontend/api/ApiPricing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useQuery, useQueryClient } from '@tanstack/react-query';
import classNames from 'classnames';
import difference from 'lodash/difference';
import find from 'lodash/find';
import React, { useContext, useEffect } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { Link, useMatch, useNavigate } from 'react-router-dom';

import { I18nContext, ModalContext } from '../../../contexts';
Expand Down Expand Up @@ -38,6 +38,7 @@ import { ApiDocumentation } from './ApiDocumentation';
import { ApiRedoc } from './ApiRedoc';
import { ApiSwagger } from './ApiSwagger';
import { ApiKeyCard } from '../../backoffice/apikeys/TeamApiKeysForApi';
import { Form, type } from '@maif/react-forms';

export const currency = (plan?: IBaseUsagePlan) => {
if (!plan) {
Expand All @@ -55,6 +56,7 @@ type ApiPricingCardProps = {
plan: IUsagePlan;
apiKey?: ISubscription;
motivation?: object;
customName?: string;
}) => Promise<void>;
myTeams: Array<ITeamSimple>;
ownerTeam: ITeamSimple;
Expand All @@ -69,14 +71,13 @@ const ApiPricingCard = (props: ApiPricingCardProps) => {
openLoginOrRegisterModal,
openApiKeySelectModal,
openCustomModal,
close,
openRightPanel
close
} = useContext(ModalContext);
const { client } = useContext(getApolloContext());

const { connectedUser, tenant } = useContext(GlobalContext);

const showApiKeySelectModal = (team: string) => {
const showApiKeySelectModal = (team: string, customName?: string) => {
const { plan } = props;

//FIXME: not bwaaagh !!
Expand All @@ -87,6 +88,7 @@ const ApiPricingCard = (props: ApiPricingCardProps) => {
const askForApikeys = (
team: string,
plan: IUsagePlan,
customName?: string,
apiKey?: ISubscription
) => {
const adminStep = plan.subscriptionProcess.find((s) =>
Expand All @@ -97,12 +99,12 @@ const ApiPricingCard = (props: ApiPricingCardProps) => {
title: translate('motivations.modal.title'),
schema: adminStep.schema,
onSubmit: (motivation: object) =>
props.askForApikeys({ team, plan, apiKey, motivation }),
props.askForApikeys({ team, plan, apiKey, motivation, customName }),
actionLabel: translate('Send'),
value: apiKey?.customMetadata,
});
} else {
props.askForApikeys({ team, plan: plan, apiKey }).then(() => close());
props.askForApikeys({ team, plan: plan, apiKey, customName }).then(() => close());
}
};

Expand Down Expand Up @@ -161,14 +163,14 @@ const ApiPricingCard = (props: ApiPricingCardProps) => {
!tenant.aggregationApiKeysSecurity || !plan.aggregationApiKeysSecurity ||
filteredApiKeys.length <= 0
) {
askForApikeys(team, plan);
askForApikeys(team, plan, customName);
} else {
openApiKeySelectModal({
plan,
apiKeys: filteredApiKeys,
onSubscribe: () => askForApikeys(team, plan),
onSubscribe: () => askForApikeys(team, plan, customName),
extendApiKey: (apiKey: ISubscription) =>
askForApikeys(team, plan, apiKey),
askForApikeys(team, plan, customName, apiKey),
});
}
}
Expand Down Expand Up @@ -229,50 +231,50 @@ const ApiPricingCard = (props: ApiPricingCardProps) => {
})
}

// const displaySubscription = () => {
// Services.getMySubscriptions(props.api._id, props.api.currentVersion)
// .then(r => {
// openRightPanel({
// title: "test",
// content: <div>
// {r.subscriptions.map(subscription => {
// return (
// <ApiKeyCard
// api={props.api}
// apiLink={""}
// statsLink={`/`}
// key={subscription.apiKey.clientId}
// subscription={{
// ...subscription,
// parentUp: false,
// planType: "",
// planName: "planname",
// apiName: "apiName",
// _humanReadableId: "hrid",
// children: []
// }}
// subscribedApis={[]}
// updateCustomName={() => Promise.resolve()}
// toggle={console.debug}
// makeUniqueApiKey={console.debug}
// deleteApiKey={console.debug}
// toggleRotation={(
// plan,
// enabled,
// rotationEvery,
// gracePeriod
// ) =>
// Promise.resolve()
// }
// regenerateSecret={console.debug}
// transferKey={console.debug}
// />
// )
// })}
// </div>
// })
// })
// }
// const displaySubscription = () => {
// Services.getMySubscriptions(props.api._id, props.api.currentVersion)
// .then(r => {
// openRightPanel({
// title: "test",
// content: <div>
// {r.subscriptions.map(subscription => {
// return (
// <ApiKeyCard
// api={props.api}
// apiLink={""}
// statsLink={`/`}
// key={subscription.apiKey.clientId}
// subscription={{
// ...subscription,
// parentUp: false,
// planType: "",
// planName: "planname",
// apiName: "apiName",
// _humanReadableId: "hrid",
// children: []
// }}
// subscribedApis={[]}
// updateCustomName={() => Promise.resolve()}
// toggle={console.debug}
// makeUniqueApiKey={console.debug}
// deleteApiKey={console.debug}
// toggleRotation={(
// plan,
// enabled,
// rotationEvery,
// gracePeriod
// ) =>
// Promise.resolve()
// }
// regenerateSecret={console.debug}
// transferKey={console.debug}
// />
// )
// })}
// </div>
// })
// })
// }

return (
<div
Expand Down Expand Up @@ -438,11 +440,13 @@ type ITeamSelector = {
pendingTeams: Array<string>;
acceptedTeams: Array<string>;
allowMultipleDemand?: boolean;
showApiKeySelectModal: (teamId: string) => void;
showApiKeySelectModal: (teamId: string, customName?: string) => void;
plan: IUsagePlan;
};

const TeamSelector = (props: ITeamSelector) => {
const [customName, setCustomName] = useState<string>();

const { translate } = useContext(I18nContext);
const { close } = useContext(ModalContext);
const navigate = useNavigate();
Expand Down Expand Up @@ -481,7 +485,7 @@ const TeamSelector = (props: ITeamSelector) => {
})}
onClick={() => {
return allowed
? props.showApiKeySelectModal(team._id)
? props.showApiKeySelectModal(team._id, customName)
: () => { };
}}
>
Expand Down Expand Up @@ -510,6 +514,22 @@ const TeamSelector = (props: ITeamSelector) => {
);
})}
</div>
<div className="modal-description mt-2">
{translate("api.pricing.subscription.custom.name.help")}
</div>
<Form
schema={{
customName: {
type: type.string,
label: null,
}
}}
onSubmit={data => setCustomName(data.customName)}
options={{
autosubmit: true,
actions: { submit: { display: false } }
}}
/>
</div>
</div>
);
Expand All @@ -526,6 +546,7 @@ type ApiPricingProps = {
plan: IUsagePlan;
apiKey?: ISubscription;
motivation?: object;
customName?: string;
}) => Promise<void>;
};

Expand Down
Loading

0 comments on commit 76e7d85

Please sign in to comment.