diff --git a/requests/Switcher API.postman_collection.json b/requests/Switcher API.postman_collection.json index 2a3a035..9f2a8e4 100644 --- a/requests/Switcher API.postman_collection.json +++ b/requests/Switcher API.postman_collection.json @@ -5764,7 +5764,7 @@ "method": "GET", "header": [], "url": { - "raw": "{{url}}/gitops/v1/account/{{gitops_domain_id}}/default", + "raw": "{{url}}/gitops/v1/account/{{gitops_domain_id}}?environment=default", "host": [ "{{url}}" ], @@ -5772,8 +5772,13 @@ "gitops", "v1", "account", - "{{gitops_domain_id}}", - "default" + "{{gitops_domain_id}}" + ], + "query": [ + { + "key": "environment", + "value": "default" + } ] } }, diff --git a/src/external/gitops.js b/src/external/gitops.js index 976c6de..12b8e3c 100644 --- a/src/external/gitops.js +++ b/src/external/gitops.js @@ -20,53 +20,69 @@ export async function createAccount(account) { }); if (response.status !== 201) { - throw new GitOpsError(`Failed to create account [${response.status}] ${JSON.stringify(response.data)}`); + throw new GitOpsError(`Failed to create account [${response.status}] ${JSON.stringify(response.data)}`, + response.status); } return response.data; } export async function updateAccount(account) { - const url = `${process.env.SWITCHER_GITOPS_URL}/account`; - const response = await axios.put(url, account, { - httpsAgent: agent(url), - headers: headers(account.domain.id) - }); - - if (response.status !== 200) { - throw new GitOpsError(`Failed to update account [${response.status}] ${JSON.stringify(response.data)}`); + try { + const url = `${process.env.SWITCHER_GITOPS_URL}/account`; + const response = await axios.put(url, account, { + httpsAgent: agent(url), + headers: headers(account.domain.id) + }); + + if (response.status !== 200) { + throw new GitOpsError(`Failed to update account [${response.status}] ${JSON.stringify(response.data)}`, + response.status); + } + + return response.data; + } catch (e) { + throw new GitOpsError(`Failed to update account: ${e.message}`, e.status); } - - return response.data; } export async function deleteAccount(domainId, environment) { - const url = `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}`; - const response = await axios.delete(url, { - httpsAgent: agent(url), - headers: headers(domainId) - }); - - if (response.status !== 204) { - throw new GitOpsError(`Failed to delete account [${response.status}] ${JSON.stringify(response.data)}`); + try { + const url = `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}`; + const response = await axios.delete(url, { + httpsAgent: agent(url), + headers: headers(domainId) + }); + + if (response.status !== 204) { + throw new GitOpsError(`Failed to delete account [${response.status}] ${JSON.stringify(response.data)}`, + response.status); + } + } catch (e) { + throw new GitOpsError(`Failed to delete account: ${e.message}`, e.status); } } export async function fetchAccounts(domainId, environment) { - const url = environment ? - `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}` : - `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}`; - - const response = await axios.get(url, { - httpsAgent: agent(url), - headers: headers(domainId) - }); - - if (response.status !== 200) { - throw new GitOpsError(`Failed to fetch accounts [${response.status}] ${JSON.stringify(response.data)}`); + try { + const url = environment ? + `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}/${environment}` : + `${process.env.SWITCHER_GITOPS_URL}/account/${domainId}`; + + const response = await axios.get(url, { + httpsAgent: agent(url), + headers: headers(domainId) + }); + + if (response.status !== 200) { + throw new GitOpsError(`Failed to fetch accounts [${response.status}] ${JSON.stringify(response.data)}`, + response.status); + } + + return response.data; + } catch (e) { + throw new GitOpsError(`Failed to fetch accounts: ${e.message}`, e.status); } - - return response.data; } function generateToken(subject) { @@ -81,9 +97,10 @@ function generateToken(subject) { } export class GitOpsError extends Error { - constructor(message) { + constructor(message, code = 500) { super(message); this.name = this.constructor.name; + this.code = code; Error.captureStackTrace(this, this.constructor); } } \ No newline at end of file diff --git a/tests/gitops-account.test.js b/tests/gitops-account.test.js index f9fa125..3025e9a 100644 --- a/tests/gitops-account.test.js +++ b/tests/gitops-account.test.js @@ -137,7 +137,7 @@ describe('GitOps Account - Subscribe', () => { .post('/gitops/v1/account/subscribe') .set('Authorization', `Bearer ${adminMasterAccountToken}`) .send(VALID_SUBSCRIPTION_REQUEST) - .expect(500); + .expect(401); // assert expect(req.body.error).toBe('Account subscription failed');