Skip to content

Commit 0774738

Browse files
committed
some adjusts
1 parent 6488ad4 commit 0774738

File tree

3 files changed

+87
-175
lines changed

3 files changed

+87
-175
lines changed

components/onelogin/actions/create-user/create-user.mjs

Lines changed: 26 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import { parseObject } from "../../common/utils.mjs";
13
import onelogin from "../../onelogin.app.mjs";
2-
import { axios } from "@pipedream/platform";
34

45
export default {
56
key: "onelogin-create-user",
@@ -136,95 +137,32 @@ export default {
136137
},
137138
async run({ $ }) {
138139
if (!this.email && !this.username) {
139-
throw new Error("Either email or username must be provided.");
140+
throw new ConfigurationError("Either email or username must be provided.");
140141
}
141142

142-
const userData = {
143-
firstname: this.firstname,
144-
lastname: this.lastname,
145-
...(this.email
146-
? {
147-
email: this.email,
148-
}
149-
: {}),
150-
...(this.username
151-
? {
152-
username: this.username,
153-
}
154-
: {}),
155-
...(this.company != null
156-
? {
157-
company: this.company,
158-
}
159-
: {}),
160-
...(this.department != null
161-
? {
162-
department: this.department,
163-
}
164-
: {}),
165-
...(this.directoryId != null
166-
? {
167-
directory_id: this.directoryId,
168-
}
169-
: {}),
170-
...(this.distinguishedName != null
171-
? {
172-
distinguished_name: this.distinguishedName,
173-
}
174-
: {}),
175-
...(this.externalId != null
176-
? {
177-
external_id: this.externalId,
178-
}
179-
: {}),
180-
...(this.groupId != null
181-
? {
182-
group_id: parseInt(this.groupId, 10),
183-
}
184-
: {}),
185-
...(this.invalidLoginAttempts != null
186-
? {
187-
invalid_login_attempts: this.invalidLoginAttempts,
188-
}
189-
: {}),
190-
...(this.localeCode != null
191-
? {
192-
locale_code: this.localeCode,
193-
}
194-
: {}),
195-
...(this.memberOf != null
196-
? {
197-
member_of: this.memberOf,
198-
}
199-
: {}),
200-
...(this.openidName != null
201-
? {
202-
openid_name: this.openidName,
203-
}
204-
: {}),
205-
...(this.phone != null
206-
? {
207-
phone: this.phone,
208-
}
209-
: {}),
210-
...(this.samaccountname != null
211-
? {
212-
samaccountname: this.samaccountname,
213-
}
214-
: {}),
215-
...(this.title != null
216-
? {
217-
title: this.title,
218-
}
219-
: {}),
220-
...(this.customAttributes != null
221-
? {
222-
custom_attributes: this.customAttributes,
223-
}
224-
: {}),
225-
};
226-
227-
const response = await this.onelogin.createUser(userData);
143+
const response = await this.onelogin.createUser({
144+
$,
145+
data: {
146+
firstname: this.firstname,
147+
lastname: this.lastname,
148+
email: this.email,
149+
username: this.username,
150+
company: this.company,
151+
department: this.department,
152+
directory_id: this.directoryId,
153+
distinguished_name: this.distinguishedName,
154+
external_id: this.externalId,
155+
group_id: this.groupId,
156+
invalid_login_attempts: this.invalidLoginAttempts,
157+
locale_code: this.localeCode,
158+
member_of: this.memberOf,
159+
openid_name: this.openidName,
160+
phone: this.phone,
161+
samaccountname: this.samaccountname,
162+
title: this.title,
163+
custom_attributes: parseObject(this.customAttributes),
164+
},
165+
});
228166

229167
$.export("$summary", `Created user ${response.username} with ID ${response.id}`);
230168
return response;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/onelogin/onelogin.app.mjs

Lines changed: 37 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default {
5959
optional: true,
6060
},
6161
groupId: {
62-
type: "string",
62+
type: "integer",
6363
label: "Group ID",
6464
description: "Group to which the user belongs.",
6565
async options() {
@@ -221,38 +221,34 @@ export default {
221221
},
222222
},
223223
methods: {
224-
// Existing method
225-
authKeys() {
226-
console.log(Object.keys(this.$auth));
227-
},
228-
// Base URL
229224
_baseUrl() {
230-
return "https://api.onelogin.com/api/1";
231-
},
232-
// Make Request
233-
async _makeRequest(opts = {}) {
234-
const {
235-
$ = this,
236-
method = "GET",
237-
path = "/",
238-
data,
239-
params,
240-
headers = {},
241-
...otherOpts
242-
} = opts;
243-
return axios($, {
244-
method,
225+
return `https://${this.$auth.subdomain}.onelogin.com/api/1`;
226+
},
227+
_headers() {
228+
return {
229+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
230+
};
231+
},
232+
_makeRequest({
233+
$ = this, path, ...opts
234+
}) {
235+
const config = {
245236
url: this._baseUrl() + path,
246-
data: data,
247-
params: params,
248-
headers: {
249-
...headers,
250-
"Authorization": `bearer:${this.$auth.access_token}`,
251-
"Content-Type": "application/json",
252-
},
253-
...otherOpts,
237+
headers: this._headers(),
238+
...opts,
239+
};
240+
console.log("config: ", config);
241+
return axios($, config);
242+
},
243+
createUser(data, opts = {}) {
244+
return this._makeRequest({
245+
method: "POST",
246+
path: "/users",
247+
data,
248+
...opts,
254249
});
255250
},
251+
256252
// Pagination Logic
257253
async paginate(fn, ...args) {
258254
const results = [];
@@ -275,45 +271,24 @@ export default {
275271
},
276272
// List Groups
277273
async listGroups(opts = {}) {
278-
return this.paginate(async ({ page }) => {
279-
const response = await this._makeRequest({
280-
path: "/groups",
281-
params: {
282-
limit: 100,
283-
page,
284-
...opts.params,
285-
},
286-
});
287-
return response;
288-
}, opts);
274+
return this._makeRequest({
275+
path: "/groups",
276+
...opts,
277+
});
289278
},
290279
// List Roles
291280
async listRoles(opts = {}) {
292-
return this.paginate(async ({ page }) => {
293-
const response = await this._makeRequest({
294-
path: "/roles",
295-
params: {
296-
limit: 100,
297-
page,
298-
...opts.params,
299-
},
300-
});
301-
return response;
302-
}, opts);
281+
return this._makeRequest({
282+
path: "/roles",
283+
...opts,
284+
});
303285
},
304286
// List Users
305287
async listUsers(opts = {}) {
306-
return this.paginate(async ({ page }) => {
307-
const response = await this._makeRequest({
308-
path: "/users",
309-
params: {
310-
limit: 100,
311-
page,
312-
...opts.params,
313-
},
314-
});
315-
return response;
316-
}, opts);
288+
return this._makeRequest({
289+
path: "/users",
290+
...opts,
291+
});
317292
},
318293
// List Directories
319294
async listDirectories(opts = {}) {
@@ -329,15 +304,6 @@ export default {
329304
return response;
330305
}, opts);
331306
},
332-
// Create User
333-
async createUser(data, opts = {}) {
334-
return this._makeRequest({
335-
method: "POST",
336-
path: "/users",
337-
data,
338-
...opts,
339-
});
340-
},
341307
// Update User
342308
async updateUser(userId, data, opts = {}) {
343309
return this._makeRequest({
@@ -355,21 +321,5 @@ export default {
355321
...opts,
356322
});
357323
},
358-
// Emit User Created Event
359-
async emitUserCreatedEvent(filters) {
360-
// Implementation to emit event based on filters
361-
// This would typically involve setting up a webhook or listening to API events
362-
},
363-
// Emit Login Attempt Event
364-
async emitLoginAttemptEvent(filters) {
365-
// Implementation to emit event based on filters
366-
// This would typically involve setting up a webhook or listening to API events
367-
},
368-
// Emit Directory Sync Event
369-
async emitDirectorySyncEvent(filters) {
370-
// Implementation to emit event based on filters
371-
// This would typically involve setting up a webhook or listening to API events
372-
},
373324
},
374-
version: "0.0.ts",
375325
};

0 commit comments

Comments
 (0)