Skip to content

Commit

Permalink
Merge pull request #9 from oracle-samples/feature-create-activity
Browse files Browse the repository at this point in the history
  • Loading branch information
btoron authored Sep 28, 2023
2 parents bdb3939 + 0b2db33 commit 6e378c6
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"name": "@ofs-users/proxy",
"type": "module",
"version": "1.3.1",
"version": "1.4.0",
"description": "A Javascript proxy to access Oracle Field Service via REST API",
"main": "dist/ofs.es.js",
"module": "dist/ofs.es.js",
Expand Down Expand Up @@ -66,4 +66,4 @@
"dependencies": {
"tslib": "^2.4.1"
}
}
}
85 changes: 84 additions & 1 deletion src/OFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,43 @@ export class OFS {
return fetchPromise;
}

private _post(partialURL: string, data: any): Promise<OFSResponse> {
var theURL = new URL(partialURL, this._baseURL);
var myHeaders = new Headers();
myHeaders.append("Authorization", this.authorization);
myHeaders.append("Content-Type", "application/json");
var requestOptions: RequestInit = {
method: "POST",
headers: myHeaders,
body: JSON.stringify(data),
};
const fetchPromise = fetch(theURL, requestOptions)
.then(async function (response) {
// Your code for handling the data you get from the API
if (response.status < 400) {
var data = await response.json();
return new OFSResponse(
theURL,
response.status,
undefined,
data
);
} else {
return new OFSResponse(
theURL,
response.status,
response.statusText,
await response.json()
);
}
})
.catch((error) => {
console.log("error", error);
return new OFSResponse(theURL, -1);
});
return fetchPromise;
}

private _postMultiPart(
partialURL: string,
data: FormData
Expand Down Expand Up @@ -174,12 +211,57 @@ export class OFS {
return fetchPromise;
}

// Specific functions
private _delete(partialURL: string): Promise<OFSResponse> {
var theURL = new URL(partialURL, this._baseURL);
var myHeaders = new Headers();
myHeaders.append("Authorization", this.authorization);
var requestOptions = {
method: "DELETE",
headers: myHeaders,
};
const fetchPromise = fetch(theURL, requestOptions)
.then(async function (response) {
// Your code for handling the data you get from the API
if (response.status == 204) {
return new OFSResponse(
theURL,
response.status,
undefined,
undefined
);
} else {
return new OFSResponse(
theURL,
response.status,
response.statusText,
await response.json()
);
}
})
.catch((error) => {
console.log("error", error);
return new OFSResponse(theURL, -1);
});
return fetchPromise;
}

// Core: Subscription Management
async getSubscriptions(): Promise<OFSSubscriptionResponse> {
const partialURL = "/rest/ofscCore/v1/events/subscriptions";
return this._get(partialURL);
}

// Core: Activity Management
async createActivity(data: any): Promise<OFSResponse> {
const partialURL = "/rest/ofscCore/v1/activities";
return this._post(partialURL, data);
}

async deleteActivity(aid: number): Promise<OFSResponse> {
const partialURL = `/rest/ofscCore/v1/activities/${aid}`;
return this._delete(partialURL);
}

async getActivityDetails(aid: number): Promise<OFSActivityResponse> {
const partialURL = `/rest/ofscCore/v1/activities/${aid}`;
return this._get(partialURL);
Expand All @@ -189,6 +271,7 @@ export class OFS {
return this._patch(partialURL, data);
}

// Metadata: Plugin Management
async importPlugins(file?: PathLike, data?: string): Promise<OFSResponse> {
const partialURL =
"/rest/ofscMetadata/v1/plugins/custom-actions/import";
Expand Down
42 changes: 39 additions & 3 deletions test/general/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ import { OFSCredentials } from "../../src/model";
import { OFS } from "../../src/OFS";
import myCredentials from "../credentials_test.json";

const myProxy: OFS = new OFS(myCredentials);
var myProxy: OFS;

test("Creation", () => {
//const myProxy = new OFS(myCredentials);
// Setup info
beforeAll(() => {
myProxy = new OFS(myCredentials);
expect(myProxy.instance).toBe(myCredentials.instance);
});

// Teardown info
var activityList: number[] = [];
afterAll(() => {
activityList.forEach(async (aid) => {
await myProxy.deleteActivity(aid);
});
});

test("Get Subscriptions", async () => {
//const myProxy = new OFS(myCredentials);
var result = await myProxy.getSubscriptions();
Expand All @@ -33,6 +42,33 @@ test("Get non valid Activity Details", async () => {
expect(result.status).toBe(400);
});

test("Create Activity", async () => {
var activityData = {
activityType: "01",
resourceId: "FLUSA",
};
var result = await myProxy.createActivity(activityData);
expect(result.status).toBe(201);
expect(result.data.activityType).toBe(activityData.activityType);
// For cleanup
var activityId = result.data.activityId;
activityList.push(activityId);
});

test("Delete Activity", async () => {
var activityData = {
activityType: "01",
resourceId: "FLUSA",
};
var result = await myProxy.createActivity(activityData);
expect(result.status).toBe(201);
expect(result.data.activityType).toBe(activityData.activityType);
var activityId = result.data.activityId;
activityList.push(activityId);
var result = await myProxy.deleteActivity(activityId);
expect(result.status).toBe(204);
});

test("Update Activity Details", async () => {
var aid = 3954799;
var initialName = "Gizella Quintero";
Expand Down

0 comments on commit 6e378c6

Please sign in to comment.