Skip to content

Commit 6e378c6

Browse files
authored
Merge pull request #9 from oracle-samples/feature-create-activity
2 parents bdb3939 + 0b2db33 commit 6e378c6

File tree

4 files changed

+127
-8
lines changed

4 files changed

+127
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
],
66
"name": "@ofs-users/proxy",
77
"type": "module",
8-
"version": "1.3.1",
8+
"version": "1.4.0",
99
"description": "A Javascript proxy to access Oracle Field Service via REST API",
1010
"main": "dist/ofs.es.js",
1111
"module": "dist/ofs.es.js",
@@ -66,4 +66,4 @@
6666
"dependencies": {
6767
"tslib": "^2.4.1"
6868
}
69-
}
69+
}

src/OFS.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,43 @@ export class OFS {
123123
return fetchPromise;
124124
}
125125

126+
private _post(partialURL: string, data: any): Promise<OFSResponse> {
127+
var theURL = new URL(partialURL, this._baseURL);
128+
var myHeaders = new Headers();
129+
myHeaders.append("Authorization", this.authorization);
130+
myHeaders.append("Content-Type", "application/json");
131+
var requestOptions: RequestInit = {
132+
method: "POST",
133+
headers: myHeaders,
134+
body: JSON.stringify(data),
135+
};
136+
const fetchPromise = fetch(theURL, requestOptions)
137+
.then(async function (response) {
138+
// Your code for handling the data you get from the API
139+
if (response.status < 400) {
140+
var data = await response.json();
141+
return new OFSResponse(
142+
theURL,
143+
response.status,
144+
undefined,
145+
data
146+
);
147+
} else {
148+
return new OFSResponse(
149+
theURL,
150+
response.status,
151+
response.statusText,
152+
await response.json()
153+
);
154+
}
155+
})
156+
.catch((error) => {
157+
console.log("error", error);
158+
return new OFSResponse(theURL, -1);
159+
});
160+
return fetchPromise;
161+
}
162+
126163
private _postMultiPart(
127164
partialURL: string,
128165
data: FormData
@@ -174,12 +211,57 @@ export class OFS {
174211
return fetchPromise;
175212
}
176213

177-
// Specific functions
214+
private _delete(partialURL: string): Promise<OFSResponse> {
215+
var theURL = new URL(partialURL, this._baseURL);
216+
var myHeaders = new Headers();
217+
myHeaders.append("Authorization", this.authorization);
218+
var requestOptions = {
219+
method: "DELETE",
220+
headers: myHeaders,
221+
};
222+
const fetchPromise = fetch(theURL, requestOptions)
223+
.then(async function (response) {
224+
// Your code for handling the data you get from the API
225+
if (response.status == 204) {
226+
return new OFSResponse(
227+
theURL,
228+
response.status,
229+
undefined,
230+
undefined
231+
);
232+
} else {
233+
return new OFSResponse(
234+
theURL,
235+
response.status,
236+
response.statusText,
237+
await response.json()
238+
);
239+
}
240+
})
241+
.catch((error) => {
242+
console.log("error", error);
243+
return new OFSResponse(theURL, -1);
244+
});
245+
return fetchPromise;
246+
}
247+
248+
// Core: Subscription Management
178249
async getSubscriptions(): Promise<OFSSubscriptionResponse> {
179250
const partialURL = "/rest/ofscCore/v1/events/subscriptions";
180251
return this._get(partialURL);
181252
}
182253

254+
// Core: Activity Management
255+
async createActivity(data: any): Promise<OFSResponse> {
256+
const partialURL = "/rest/ofscCore/v1/activities";
257+
return this._post(partialURL, data);
258+
}
259+
260+
async deleteActivity(aid: number): Promise<OFSResponse> {
261+
const partialURL = `/rest/ofscCore/v1/activities/${aid}`;
262+
return this._delete(partialURL);
263+
}
264+
183265
async getActivityDetails(aid: number): Promise<OFSActivityResponse> {
184266
const partialURL = `/rest/ofscCore/v1/activities/${aid}`;
185267
return this._get(partialURL);
@@ -189,6 +271,7 @@ export class OFS {
189271
return this._patch(partialURL, data);
190272
}
191273

274+
// Metadata: Plugin Management
192275
async importPlugins(file?: PathLike, data?: string): Promise<OFSResponse> {
193276
const partialURL =
194277
"/rest/ofscMetadata/v1/plugins/custom-actions/import";

test/general/base.test.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ import { OFSCredentials } from "../../src/model";
88
import { OFS } from "../../src/OFS";
99
import myCredentials from "../credentials_test.json";
1010

11-
const myProxy: OFS = new OFS(myCredentials);
11+
var myProxy: OFS;
1212

13-
test("Creation", () => {
14-
//const myProxy = new OFS(myCredentials);
13+
// Setup info
14+
beforeAll(() => {
15+
myProxy = new OFS(myCredentials);
1516
expect(myProxy.instance).toBe(myCredentials.instance);
1617
});
1718

19+
// Teardown info
20+
var activityList: number[] = [];
21+
afterAll(() => {
22+
activityList.forEach(async (aid) => {
23+
await myProxy.deleteActivity(aid);
24+
});
25+
});
26+
1827
test("Get Subscriptions", async () => {
1928
//const myProxy = new OFS(myCredentials);
2029
var result = await myProxy.getSubscriptions();
@@ -33,6 +42,33 @@ test("Get non valid Activity Details", async () => {
3342
expect(result.status).toBe(400);
3443
});
3544

45+
test("Create Activity", async () => {
46+
var activityData = {
47+
activityType: "01",
48+
resourceId: "FLUSA",
49+
};
50+
var result = await myProxy.createActivity(activityData);
51+
expect(result.status).toBe(201);
52+
expect(result.data.activityType).toBe(activityData.activityType);
53+
// For cleanup
54+
var activityId = result.data.activityId;
55+
activityList.push(activityId);
56+
});
57+
58+
test("Delete Activity", async () => {
59+
var activityData = {
60+
activityType: "01",
61+
resourceId: "FLUSA",
62+
};
63+
var result = await myProxy.createActivity(activityData);
64+
expect(result.status).toBe(201);
65+
expect(result.data.activityType).toBe(activityData.activityType);
66+
var activityId = result.data.activityId;
67+
activityList.push(activityId);
68+
var result = await myProxy.deleteActivity(activityId);
69+
expect(result.status).toBe(204);
70+
});
71+
3672
test("Update Activity Details", async () => {
3773
var aid = 3954799;
3874
var initialName = "Gizella Quintero";

0 commit comments

Comments
 (0)