Skip to content

Commit e7729b8

Browse files
committed
Remove unused getSurvey method and add New Survey Response source with event emission capabilities
1 parent 09ad4db commit e7729b8

File tree

3 files changed

+159
-6
lines changed

3 files changed

+159
-6
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import surveybot from "../../surveybot.app.mjs";
3+
import sampleEmit from "./test-event.mjs";
4+
5+
export default {
6+
key: "surveybot-new-survey-response",
7+
name: "New Survey Response",
8+
description: "Emit new event when a new survey response is received. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
surveybot,
14+
db: "$.service.db",
15+
timer: {
16+
type: "$.interface.timer",
17+
default: {
18+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
19+
},
20+
},
21+
surveyId: {
22+
propDefinition: [
23+
surveybot,
24+
"surveyId",
25+
],
26+
},
27+
},
28+
methods: {
29+
_getLastDate() {
30+
return this.db.get("lastDate") || 0;
31+
},
32+
_setLastDate(lastDate) {
33+
this.db.set("lastDate", lastDate);
34+
},
35+
async emitEvent(maxResults = false) {
36+
const lastDate = this._getLastDate();
37+
const data = await this.surveybot.getSurveyResponses({
38+
surveyId: this.surveyId,
39+
});
40+
41+
let responses = data.responses;
42+
43+
responses = responses.filter((response) => Date.parse(response.started_at) > lastDate);
44+
45+
if (responses.length) {
46+
if (maxResults && (responses.length > maxResults)) {
47+
responses.length = maxResults;
48+
}
49+
this._setLastDate(Date.parse(responses[0].started_at));
50+
}
51+
52+
for (const item of responses.reverse()) {
53+
this.$emit(item, {
54+
id: item.id,
55+
summary: `New response for survey ${this.surveyId} by ${item.respondent.name}`,
56+
ts: Date.parse(item.started_at),
57+
});
58+
}
59+
},
60+
},
61+
hooks: {
62+
async deploy() {
63+
await this.emitEvent(25);
64+
},
65+
},
66+
async run() {
67+
await this.emitEvent();
68+
},
69+
sampleEmit,
70+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
export default {
2+
"completed":true,
3+
"started_at":"03/11/2025 03:45 PM",
4+
"completed_at":"03/11/2025 03:46 PM",
5+
"id":"29360C9E3A5F5E8F61BFE425A3C8BC4E",
6+
"respondent":{
7+
"id":1789117,
8+
"name":"John Doe",
9+
"first_name":"John",
10+
"last_name":"Doe",
11+
"gender":"male",
12+
"locale":"pt_BR",
13+
"timezone":null,
14+
"email":null,
15+
"picture":null,
16+
"date_of_birth":null,
17+
"occupation":null,
18+
"address":null,
19+
"relationship_status":null,
20+
"phone_number":null,
21+
"mobile_number":null,
22+
"tags":"",
23+
"location":null,
24+
"city":null,
25+
"country":null,
26+
"attributes":[
27+
28+
]
29+
},
30+
"quiz_score":0,
31+
"answers":[
32+
{
33+
"question_id":217859,
34+
"question_text":"content removed",
35+
"content":null
36+
},
37+
{
38+
"question_id":217860,
39+
"question_text":"Which country is the largest producer of rubber?",
40+
"content":"Thailand"
41+
},
42+
{
43+
"question_id":217861,
44+
"question_text":"content removed",
45+
"content":null
46+
},
47+
{
48+
"question_id":217862,
49+
"question_text":"Which country is also known as the land of the rising sun?",
50+
"content":"Japan"
51+
},
52+
{
53+
"question_id":217863,
54+
"question_text":"content removed",
55+
"content":null
56+
},
57+
{
58+
"question_id":217864,
59+
"question_text":"In which country was Lord of the Rings filmed?",
60+
"content":"New Zealand"
61+
},
62+
{
63+
"question_id":217865,
64+
"question_text":"content removed",
65+
"content":null
66+
},
67+
{
68+
"question_id":217866,
69+
"question_text":"What was the name of Alexander's horse?",
70+
"content":"Thunderbolt "
71+
},
72+
{
73+
"question_id":217867,
74+
"question_text":"content removed",
75+
"content":null
76+
},
77+
{
78+
"question_id":217868,
79+
"question_text":"Which Shakespeare play does the line ''The lady doth protest too much, methinks'' come from?",
80+
"content":"Macbeth"
81+
},
82+
{
83+
"question_id":217869,
84+
"question_text":"content removed",
85+
"content":null
86+
}
87+
],
88+
"shared_by_respondent":null
89+
}

components/surveybot/surveybot.app.mjs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ export default {
6767
...args,
6868
});
6969
},
70-
getSurvey(args = {}) {
71-
return this._makeRequest({
72-
path: "surveys",
73-
...args,
74-
});
75-
},
7670
getSurveyResponses({
7771
surveyId, ...args
7872
}) {

0 commit comments

Comments
 (0)