Skip to content

Commit

Permalink
Merge pull request #228 from Scalingo/feat/contract-agreements
Browse files Browse the repository at this point in the history
feat: support for contracts and agreements
  • Loading branch information
ksol authored Jun 20, 2022
2 parents 7673c84 + 529b09a commit a05d192
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Unreleased

* nothing yet
## 0.6.1

* feature: support for contracts and agreements

## 0.6.0

Expand Down
70 changes: 70 additions & 0 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Client } from "..";
import { Contract, ContractAgreement } from "../models/auth/contracts";
import { CreateAgreementParams } from "../params/auth/contracts";
import { unpackData } from "../utils";

/**
* Contract and Agreements API Client
*/
export default class Contracts {
/** Scalingo API Client */
_client: Client;

/**
* Create a new "thematic" client
* @param client Scalingo API Client
*/
constructor(client: Client) {
this._client = client;
}

/**
* Get all existing contracts
*/
all(): Promise<Contract[]> {
return unpackData(
this._client.authApiClient().get("/contracts"),
"contracts"
);
}

/**
* Get all existing contract agreements
*/
allAgreements(): Promise<ContractAgreement[]> {
return unpackData(
this._client.authApiClient().get("/contract_agreements"),
"contract_agreements"
);
}

/**
* Show a given contract
*/
show(id: string): Promise<Contract> {
return unpackData(
this._client.authApiClient().get(`/contracts/${id}`),
"contract"
);
}

/**
* Show a given contract agreement
*/
showAgreement(id: string): Promise<ContractAgreement> {
return unpackData(
this._client.authApiClient().get(`/contract_agreements/${id}`),
"contract_agreement"
);
}

/** Create an agreement for the given contract */
createAgreement(params: CreateAgreementParams): Promise<ContractAgreement> {
return unpackData(
this._client
.authApiClient()
.post(`/contract_agreements`, { contract_agreement: params }),
"contract_agreement"
);
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Autoscalers from "./Autoscalers";
import Billing from "./Billing";
import Collaborators from "./Collaborators";
import Containers from "./Containers";
import Contracts from "./Contracts";
import CronTasks from "./CronTasks";
import Deployments from "./Deployments";
import Domains from "./Domains";
Expand Down Expand Up @@ -88,6 +89,7 @@ export class Client {
Billing = new Billing(this);
Collaborators = new Collaborators(this);
Containers = new Containers(this);
Contracts = new Contracts(this);
CronTasks = new CronTasks(this);
Deployments = new Deployments(this);
Domains = new Domains(this);
Expand Down
29 changes: 29 additions & 0 deletions src/models/auth/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export type ContractLangs = "en" | "fr";

export interface Contract {
/** Unique key ID */
id: string;
/** Contract kind */
kind: string;
/** Version of the contract */
version: string;
/** Date at which the contract starts applying */
start_date: string;
/** Date at which the contract stops applying, if relevant */
end_date: string | null;
/** Map of locale => url of the pdf of the contract in locale */
pdf_urls: Record<ContractLangs, string>;
/** Map of locale => url of a web page of the contract in locale */
web_urls: Record<ContractLangs, string>;
}

export interface ContractAgreement {
/** Unique key ID */
id: string;
/** ID of the user who accepted the contract */
user_id: string;
/** The date when the contract was accepted */
date: string;
/** The locale in which the contract was accepted */
locale: ContractLangs;
}
3 changes: 3 additions & 0 deletions src/models/auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export interface User {
suspension_reason: string | null;

preferences: UserPreferences;

contracts: Record<string, boolean>;
new_contracts: Record<string, boolean>;
}

export interface GithubProfile {
Expand Down
8 changes: 8 additions & 0 deletions src/params/auth/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ContractLangs } from "../../models/auth/contracts";

export interface CreateAgreementParams {
/** ID of the contract to agree to */
contract_id: string;
/** Locale of the contract to agree to */
locale: ContractLangs;
}
61 changes: 61 additions & 0 deletions test/Contracts/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Contracts from "../../src/Contracts";
import { testGetter, testPost } from "../utils/http";

describe("Contracts#all", () => {
testGetter(
"https://auth.scalingo.com/v1/contracts",
null,
"contracts",
(client) => {
return new Contracts(client).all();
}
);
});

describe("Contracts#allAgreements", () => {
testGetter(
"https://auth.scalingo.com/v1/contract_agreements",
null,
"contract_agreements",
(client) => {
return new Contracts(client).allAgreements();
}
);
});

describe("Contracts#show", () => {
testGetter(
"https://auth.scalingo.com/v1/contracts/contract-id",
null,
"contract",
(client) => {
return new Contracts(client).show("contract-id");
}
);
});

describe("Contracts#showAgreement", () => {
testGetter(
"https://auth.scalingo.com/v1/contract_agreements/contract-id",
null,
"contract_agreement",
(client) => {
return new Contracts(client).showAgreement("contract-id");
}
);
});

describe("Contracts#createAgreement", () => {
testPost(
"https://auth.scalingo.com/v1/contract_agreements",
null,
{ contract_agreement: { contract_id: "my-id", locale: "es" } },
"contract_agreement",
(client) => {
return new Contracts(client).createAgreement({
contract_id: "my-id",
locale: "es",
});
}
);
});

0 comments on commit a05d192

Please sign in to comment.