Skip to content

Commit 28a5961

Browse files
authored
Merging pull request #14766
* [ACTION] Learnworlds - Enroll User #14707 Actions - Enroll User * pnpm update
1 parent 7397ceb commit 28a5961

File tree

5 files changed

+191
-7
lines changed

5 files changed

+191
-7
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { PRODUCT_TYPE_OPTIONS } from "../../common/constants.mjs";
2+
import learnworlds from "../../learnworlds.app.mjs";
3+
4+
export default {
5+
key: "learnworlds-enroll-user",
6+
name: "Enroll User",
7+
description: "Enroll user to product. [See the documentation](https://www.learnworlds.dev/docs/api/3d5e79f96b44a-enroll-user-to-product)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
learnworlds,
12+
userId: {
13+
propDefinition: [
14+
learnworlds,
15+
"userId",
16+
],
17+
},
18+
productType: {
19+
type: "string",
20+
label: "Product Type",
21+
description: "Type of the product.",
22+
options: PRODUCT_TYPE_OPTIONS,
23+
reloadProps: true,
24+
},
25+
justification: {
26+
type: "string",
27+
label: "Justification",
28+
description: "Any justification/note for the enrollment.",
29+
optional: true,
30+
},
31+
sendEnrollmentEmail: {
32+
type: "boolean",
33+
label: "Send Enrollment Email",
34+
description: "Indication about whether the user should receive the enrollment email; true if she should receive the email, false if she should not.",
35+
optional: true,
36+
},
37+
},
38+
methods: {
39+
capitalize(s) {
40+
return String(s[0]).toUpperCase() + String(s).slice(1);
41+
},
42+
},
43+
async additionalProps() {
44+
const props = {};
45+
if (this.productType) {
46+
props.productId = {
47+
type: "string",
48+
label: "Product Id",
49+
description: "Unique Identifier of the product.",
50+
options: async({ page }) => {
51+
const { data } = await this.learnworlds[`list${this.capitalize(this.productType)}s`]({
52+
params: {
53+
page: page + 1,
54+
},
55+
});
56+
57+
return data.map(({
58+
id: value, title: label,
59+
}) => ({
60+
label,
61+
value,
62+
}));
63+
},
64+
};
65+
}
66+
return props;
67+
},
68+
async run({ $ }) {
69+
const product = await this.learnworlds[`get${this.capitalize(this.productType)}`]({
70+
productId: this.productId,
71+
});
72+
73+
const response = await this.learnworlds.enrollUser({
74+
$,
75+
userId: this.userId,
76+
data: {
77+
productId: this.productId,
78+
productType: this.productType,
79+
justification: this.justification,
80+
price: product.price || product.original_price,
81+
send_enrollment_email: this.sendEnrollmentEmail,
82+
},
83+
});
84+
85+
$.export("$summary", "User successfully enrolled!");
86+
return response;
87+
},
88+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const PRODUCT_TYPE_OPTIONS = [
2+
"course",
3+
"bundle",
4+
"subscription",
5+
];
Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,95 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "learnworlds",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
userId: {
8+
type: "string",
9+
label: "User Id",
10+
description: "Unique identifier of the used.",
11+
async options({ page }) {
12+
const { data } = await this.listUsers({
13+
params: {
14+
page: page + 1,
15+
},
16+
});
17+
18+
return data.map(({
19+
id: value, username: label,
20+
}) => ({
21+
label,
22+
value,
23+
}));
24+
},
25+
},
26+
},
527
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
28+
_baseUrl() {
29+
return `https://${this.$auth.school_domain}/admin/api/v2`;
30+
},
31+
_getHeaders() {
32+
return {
33+
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
34+
"Lw-Client": `${this.$auth.oauth_client_id}`,
35+
};
36+
},
37+
_makeRequest({
38+
$ = this, path, ...opts
39+
}) {
40+
return axios($, {
41+
url: this._baseUrl() + path,
42+
headers: this._getHeaders(),
43+
...opts,
44+
});
45+
},
46+
enrollUser({
47+
userId, ...opts
48+
}) {
49+
return this._makeRequest({
50+
method: "POST",
51+
path: `/users/${userId}/enrollment`,
52+
...opts,
53+
});
54+
},
55+
getBundle({ productId }) {
56+
return this._makeRequest({
57+
path: `/bundles/${productId}`,
58+
});
59+
},
60+
getCourse({ productId }) {
61+
return this._makeRequest({
62+
path: `/courses/${productId}`,
63+
});
64+
},
65+
getSubscription({ productId }) {
66+
return this._makeRequest({
67+
path: `/subscription-plans/${productId}`,
68+
});
69+
},
70+
listBundles(opts = {}) {
71+
return this._makeRequest({
72+
path: "/bundles",
73+
...opts,
74+
});
75+
},
76+
listCourses(opts = {}) {
77+
return this._makeRequest({
78+
path: "/courses",
79+
...opts,
80+
});
81+
},
82+
listSubscriptions(opts = {}) {
83+
return this._makeRequest({
84+
path: "/subscription-plans",
85+
...opts,
86+
});
87+
},
88+
listUsers(opts = {}) {
89+
return this._makeRequest({
90+
path: "/users",
91+
...opts,
92+
});
993
},
1094
},
1195
};

components/learnworlds/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/learnworlds",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream LearnWorlds Components",
55
"main": "learnworlds.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
15-
}
18+
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)