diff --git a/config.json b/config.json index fb427056e5bf161d09d869bbb45a3eb8863a9726..4a2b8bb7156abc9115f1444a3c5d9deddcc72b64 100644 --- a/config.json +++ b/config.json @@ -8,8 +8,9 @@ "konnectorConfig": { "name": "enedis", "type": "ELECTRICITY", - "slug": "enedis-scraping-connector", - "id": "io.cozy.konnectors/enedis-scraping-connector" + "oauth": true, + "slug": "enedis-konnector", + "id": "io.cozy.konnectors/enedis-konnector" } }, { @@ -20,6 +21,7 @@ "konnectorConfig": { "name": "eau du grand lyon", "type": "WATER", + "oauth": false, "slug": "egl-api-connector", "id": "io.cozy.konnectors/egl-api-connector" } @@ -32,6 +34,7 @@ "konnectorConfig": { "name": "grdf", "type": "GAS", + "oauth": false, "slug": "grdf-scraping-connector", "id": "io.cozy.konnectors/grdf-scraping-connector" } diff --git a/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx b/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx index 56187b1239d3b86d805067db00d31685f851feb1..a6f72bd43b96f96435f9b962e3b7c1326f9de6b8 100644 --- a/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx +++ b/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx @@ -3,7 +3,6 @@ import { translate } from 'cozy-ui/react/I18n' import FluidConfigService from 'services/fluidConfigService' import IFluidConfig from 'services/IFluidConfig' import KonnectorViewerList from 'components/ContentComponents/KonnectorViewer/KonnectorViewerList' -import OAuthKonnector from 'components/ContentComponents/OAuthKonnector/OAuthKonnector' interface KonnectorViewerContainerProps { isParam: boolean @@ -23,7 +22,6 @@ const KonnectorViewerContainer: React.FC<KonnectorViewerContainerProps> = ({ </div> <KonnectorViewerList isParam={isParam} fluidConfigs={fluidConfigs} /> </div> - <OAuthKonnector /> </div> ) } diff --git a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx index 8f1cd9c4dd24c82f800150141e055bb9061af66b..d2b0a0bf634654b605e98cf782b7d5409a4bf4d5 100644 --- a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx +++ b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx @@ -27,6 +27,8 @@ import KonnectorService from 'services/konnectorService' import KonnectorStatusService from 'services/konnectorStatusService' import Spinner from 'components/CommonKit/Spinner/Spinner' import IFluidConfig from 'services/IFluidConfig' +import OAuthKonnector from '../OAuthKonnector/OAuthKonnector' +import { Account, Konnector, Trigger } from 'doctypes' interface KonnectorViewerCardProps { fluidConfig: IFluidConfig @@ -170,7 +172,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ) const konnectorService = new KonnectorService( client, - fluidConfig.konnectorConfig.id + fluidConfig.konnectorConfig.slug ) const konnector = await konnectorService.fetchKonnector() const triggersServices = new TriggerService(client, account, konnector) @@ -192,6 +194,45 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ } } + const initOauthAccount = async oauthAccountId => { + console.log('initOauthAccount', oauthAccountId) + setError('') + setJobState('') + setLoading(true) + try { + const account = await AccountService.getAccount(client, oauthAccountId) + const konnectorService = new KonnectorService( + client, + fluidConfig.konnectorConfig.slug + ) + console.log(account) + const konnector: Konnector = await konnectorService.fetchKonnector() + console.log(konnector) + const triggersServices = new TriggerService(client, account, konnector) + const trigger: Trigger = await triggersServices.createTrigger() + console.log(trigger) + if (!trigger) { + throw new Error(`Error during trigger creation`) + } + //Launch the creation trigger + const job = await triggersServices.launchTrigger() + if (!job) { + throw new Error(`Error during trigger launching`) + } + console.log(job) + setUpdateTrigger(trigger) + setlaunchedJob(job) + setUpdateDate(new Date(job.queued_at).toDateString()) + setAccountId(job.message.account) + setError('Connected') + setAccount(true) + await context.refreshFluidTypes() + } catch (error) { + setLoading(false) + setError(error.message) + } + } + const getIcon = (jobState: string) => { switch (jobState) { case JobState.Errored: @@ -287,63 +328,77 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ className={`accordion-content ${setActive}`} > {!isKonnectorAcc ? ( - <form - className="form" - onSubmit={(e: React.FormEvent<HTMLFormElement>) => - handleSubmit(e) - } - > - {t('KONNECTORCONFIG.LABEL_FILLIN')}{' '} - {fluidConfig.konnectorConfig.name} - <div className="form-group"> - <div className="form-message">{error}</div> - <input - id={'idFieldLogin' + type} - type="text" - className="form-control form-input" - aria-describedby="emailHelp" - placeholder="Adresse e-mail" - name="login" - onChange={(e: React.ChangeEvent<HTMLInputElement>) => - setLogin(e.target.value) - } - value={login} - ></input> - </div> - <div className="form-group"> - <div className="form-message">{error}</div> - <input - id={'idFieldPassword' + type} - type="password" - className="form-control form-input" - aria-describedby="PasswordHelp" - placeholder="Mot de passe" - name="password" - onChange={(e: React.ChangeEvent<HTMLInputElement>) => - setPassword(e.target.value) + <div> + {!fluidConfig.konnectorConfig.oauth ? ( + <form + className="form" + onSubmit={(e: React.FormEvent<HTMLFormElement>) => + handleSubmit(e) } - value={password} - /> - <span> - <StyledIconButton - icon={TrailingIcon} - className="form-trailing-icon" - size={22} - onClick={() => revealPassword('idFieldPassword' + type)} - /> - </span> - </div> - <StyledButton type="submit" color="primary" disabled={loading}> - {t('KONNECTORCONFIG.BTN_CONNECTION')} - </StyledButton> - <StyledButton - type="button" - color="secondary" - disabled={loading} - > - {t('KONNECTORCONFIG.BTN_NOACCOUNT')} - </StyledButton> - </form> + > + {t('KONNECTORCONFIG.LABEL_FILLIN')}{' '} + {fluidConfig.konnectorConfig.name} + <div className="form-group"> + <div className="form-message">{error}</div> + <input + id={'idFieldLogin' + type} + type="text" + className="form-control form-input" + aria-describedby="emailHelp" + placeholder="Adresse e-mail" + name="login" + onChange={(e: React.ChangeEvent<HTMLInputElement>) => + setLogin(e.target.value) + } + value={login} + ></input> + </div> + <div className="form-group"> + <div className="form-message">{error}</div> + <input + id={'idFieldPassword' + type} + type="password" + className="form-control form-input" + aria-describedby="PasswordHelp" + placeholder="Mot de passe" + name="password" + onChange={(e: React.ChangeEvent<HTMLInputElement>) => + setPassword(e.target.value) + } + value={password} + /> + <span> + <StyledIconButton + icon={TrailingIcon} + className="form-trailing-icon" + size={22} + onClick={() => + revealPassword('idFieldPassword' + type) + } + /> + </span> + </div> + <StyledButton + type="submit" + color="primary" + disabled={loading} + > + {t('KONNECTORCONFIG.BTN_CONNECTION')} + </StyledButton> + <StyledButton + type="button" + color="secondary" + disabled={loading} + > + {t('KONNECTORCONFIG.BTN_NOACCOUNT')} + </StyledButton> + </form> + ) : ( + <div> + <OAuthKonnector setKonnectorAccount={initOauthAccount} /> + </div> + )} + </div> ) : ( <div> <div> diff --git a/src/components/ContentComponents/OAuthKonnector/OAuthKonnector.tsx b/src/components/ContentComponents/OAuthKonnector/OAuthKonnector.tsx index 089b09b5dab1647b7dce22da90c9560d51ce90df..609b6968fece0c7459f6a4cd5b34b74bc4d8a6fb 100644 --- a/src/components/ContentComponents/OAuthKonnector/OAuthKonnector.tsx +++ b/src/components/ContentComponents/OAuthKonnector/OAuthKonnector.tsx @@ -15,12 +15,14 @@ import StyledButton from 'components/CommonKit/Button/StyledButton' interface OAuthKonnectorProps { // account: any // konnector: any + setKonnectorAccount: any client: Client t: Function } const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({ client, + setKonnectorAccount, t, }: OAuthKonnectorProps) => { const konnectorId = 'enedis-konnector' @@ -39,6 +41,8 @@ const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({ } function handleAccountId(accountId) { // const { onSuccess } = this.props + console.log('handleaccountid', accountId) + setKonnectorAccount(accountId) endOAuth() // if (typeof onSuccess === 'function') onSuccess(accountId) } diff --git a/src/services/IFluidConfig.ts b/src/services/IFluidConfig.ts index 6d8d08f784ed7f68ff34bd53854139333f90725a..473047d7441a35d61852ed91e542e9f6864d16c6 100644 --- a/src/services/IFluidConfig.ts +++ b/src/services/IFluidConfig.ts @@ -1,6 +1,7 @@ interface KonnectorConfig { name: string type: string + oauth: boolean slug: string id: string } diff --git a/src/services/accountService.ts b/src/services/accountService.ts index 569108698e0b46f6173899184325316a47d6708d..77795c9e374c6c5fd4bd037c6364764d8171e078 100644 --- a/src/services/accountService.ts +++ b/src/services/accountService.ts @@ -82,6 +82,7 @@ export class AccountService { static getAccount = async (client: Client, id: string) => { try { + console.log('id account : ', id) const account: Account = await accountsMutations(client).findAccount(id) return account } catch (error) { diff --git a/src/services/konnectorService.ts b/src/services/konnectorService.ts index b2521df2e609aac2e7a7176becb204ced0021d12..3f9b739869994b67d005060d4f6f7231175260a8 100644 --- a/src/services/konnectorService.ts +++ b/src/services/konnectorService.ts @@ -21,11 +21,13 @@ export default class KonnectorService { ) } try { + console.log('my id in service : ', this._konnectorId) const { data } = await this._client.query( this._client.find(KONNECTORS_DOCTYPE).where({ _id: KONNECTORS_DOCTYPE + '/' + this._konnectorId, }) ) + console.log('!!!!!!!!!!!!konnector service : ', data) const konnector: Konnector = data && data[0] this._konnector = konnector return konnector