sdk-kit 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="77" height="47" fill="none" aria-labelledby="vite-logo-title" viewBox="0 0 77 47"><title id="vite-logo-title">Vite</title><style>.parenthesis{fill:#000}@media (prefers-color-scheme:dark){.parenthesis{fill:#fff}}</style><path fill="#9135ff" d="M40.151 45.71c-.663.844-2.02.374-2.02-.699V34.708a2.26 2.26 0 0 0-2.262-2.262H24.493c-.92 0-1.457-1.04-.92-1.788l7.479-10.471c1.07-1.498 0-3.578-1.842-3.578H15.443c-.92 0-1.456-1.04-.92-1.788l9.696-13.576c.213-.297.556-.474.92-.474h28.894c.92 0 1.456 1.04.92 1.788l-7.48 10.472c-1.07 1.497 0 3.578 1.842 3.578h11.376c.944 0 1.474 1.087.89 1.83L40.153 45.712z"/><mask id="a" width="48" height="47" x="14" y="0" maskUnits="userSpaceOnUse" style="mask-type:alpha"><path fill="#000" d="M40.047 45.71c-.663.843-2.02.374-2.02-.699V34.708a2.26 2.26 0 0 0-2.262-2.262H24.389c-.92 0-1.457-1.04-.92-1.788l7.479-10.472c1.07-1.497 0-3.578-1.842-3.578H15.34c-.92 0-1.456-1.04-.92-1.788l9.696-13.575c.213-.297.556-.474.92-.474H53.93c.92 0 1.456 1.04.92 1.788L47.37 13.03c-1.07 1.498 0 3.578 1.842 3.578h11.376c.944 0 1.474 1.088.89 1.831L40.049 45.712z"/></mask><g mask="url(#a)"><g filter="url(#b)"><ellipse cx="5.508" cy="14.704" fill="#eee6ff" rx="5.508" ry="14.704" transform="rotate(269.814 20.96 11.29)scale(-1 1)"/></g><g filter="url(#c)"><ellipse cx="10.399" cy="29.851" fill="#eee6ff" rx="10.399" ry="29.851" transform="rotate(89.814 -16.902 -8.275)scale(1 -1)"/></g><g filter="url(#d)"><ellipse cx="5.508" cy="30.487" fill="#8900ff" rx="5.508" ry="30.487" transform="rotate(89.814 -19.197 -7.127)scale(1 -1)"/></g><g filter="url(#e)"><ellipse cx="5.508" cy="30.599" fill="#8900ff" rx="5.508" ry="30.599" transform="rotate(89.814 -25.928 4.177)scale(1 -1)"/></g><g filter="url(#f)"><ellipse cx="5.508" cy="30.599" fill="#8900ff" rx="5.508" ry="30.599" transform="rotate(89.814 -25.738 5.52)scale(1 -1)"/></g><g filter="url(#g)"><ellipse cx="14.072" cy="22.078" fill="#eee6ff" rx="14.072" ry="22.078" transform="rotate(93.35 31.245 55.578)scale(-1 1)"/></g><g filter="url(#h)"><ellipse cx="3.47" cy="21.501" fill="#8900ff" rx="3.47" ry="21.501" transform="rotate(89.009 35.419 55.202)scale(-1 1)"/></g><g filter="url(#i)"><ellipse cx="3.47" cy="21.501" fill="#8900ff" rx="3.47" ry="21.501" transform="rotate(89.009 35.419 55.202)scale(-1 1)"/></g><g filter="url(#j)"><ellipse cx="14.592" cy="9.743" fill="#8900ff" rx="4.407" ry="29.108" transform="rotate(39.51 14.592 9.743)"/></g><g filter="url(#k)"><ellipse cx="61.728" cy="-5.321" fill="#8900ff" rx="4.407" ry="29.108" transform="rotate(37.892 61.728 -5.32)"/></g><g filter="url(#l)"><ellipse cx="55.618" cy="7.104" fill="#00c2ff" rx="5.971" ry="9.665" transform="rotate(37.892 55.618 7.104)"/></g><g filter="url(#m)"><ellipse cx="12.326" cy="39.103" fill="#8900ff" rx="4.407" ry="29.108" transform="rotate(37.892 12.326 39.103)"/></g><g filter="url(#n)"><ellipse cx="12.326" cy="39.103" fill="#8900ff" rx="4.407" ry="29.108" transform="rotate(37.892 12.326 39.103)"/></g><g filter="url(#o)"><ellipse cx="49.857" cy="30.678" fill="#8900ff" rx="4.407" ry="29.108" transform="rotate(37.892 49.857 30.678)"/></g><g filter="url(#p)"><ellipse cx="52.623" cy="33.171" fill="#00c2ff" rx="5.971" ry="15.297" transform="rotate(37.892 52.623 33.17)"/></g></g><path d="M6.919 0c-9.198 13.166-9.252 33.575 0 46.789h6.215c-9.25-13.214-9.196-33.623 0-46.789zm62.424 0h-6.215c9.198 13.166 9.252 33.575 0 46.789h6.215c9.25-13.214 9.196-33.623 0-46.789" class="parenthesis"/><defs><filter id="b" width="60.045" height="41.654" x="-5.564" y="16.92" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="7.659"/></filter><filter id="c" width="90.34" height="51.437" x="-40.407" y="-6.762" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="7.659"/></filter><filter id="d" width="79.355" height="29.4" x="-35.435" y="2.801" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="e" width="79.579" height="29.4" x="-30.84" y="20.8" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="f" width="79.579" height="29.4" x="-29.307" y="21.949" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="g" width="74.749" height="58.852" x="29.961" y="-17.13" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="7.659"/></filter><filter id="h" width="61.377" height="25.362" x="37.754" y="3.055" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="i" width="61.377" height="25.362" x="37.754" y="3.055" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="j" width="56.045" height="63.649" x="-13.43" y="-22.082" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="k" width="54.814" height="64.646" x="34.321" y="-37.644" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="l" width="33.541" height="35.313" x="38.847" y="-10.552" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="m" width="54.814" height="64.646" x="-15.081" y="6.78" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="n" width="54.814" height="64.646" x="-15.081" y="6.78" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="o" width="54.814" height="64.646" x="22.45" y="-1.645" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter><filter id="p" width="39.409" height="43.623" x="32.919" y="11.36" color-interpolation-filters="sRGB" filterUnits="userSpaceOnUse"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/><feGaussianBlur result="effect1_foregroundBlur_2002_17286" stdDeviation="4.596"/></filter></defs></svg>
package/src/index.css ADDED
@@ -0,0 +1,111 @@
1
+ :root {
2
+ --text: #6b6375;
3
+ --text-h: #08060d;
4
+ --bg: #fff;
5
+ --border: #e5e4e7;
6
+ --code-bg: #f4f3ec;
7
+ --accent: #aa3bff;
8
+ --accent-bg: rgba(170, 59, 255, 0.1);
9
+ --accent-border: rgba(170, 59, 255, 0.5);
10
+ --social-bg: rgba(244, 243, 236, 0.5);
11
+ --shadow:
12
+ rgba(0, 0, 0, 0.1) 0 10px 15px -3px, rgba(0, 0, 0, 0.05) 0 4px 6px -2px;
13
+
14
+ --sans: system-ui, 'Segoe UI', Roboto, sans-serif;
15
+ --heading: system-ui, 'Segoe UI', Roboto, sans-serif;
16
+ --mono: ui-monospace, Consolas, monospace;
17
+
18
+ font: 18px/145% var(--sans);
19
+ letter-spacing: 0.18px;
20
+ color-scheme: light dark;
21
+ color: var(--text);
22
+ background: var(--bg);
23
+ font-synthesis: none;
24
+ text-rendering: optimizeLegibility;
25
+ -webkit-font-smoothing: antialiased;
26
+ -moz-osx-font-smoothing: grayscale;
27
+
28
+ @media (max-width: 1024px) {
29
+ font-size: 16px;
30
+ }
31
+ }
32
+
33
+ @media (prefers-color-scheme: dark) {
34
+ :root {
35
+ --text: #9ca3af;
36
+ --text-h: #f3f4f6;
37
+ --bg: #16171d;
38
+ --border: #2e303a;
39
+ --code-bg: #1f2028;
40
+ --accent: #c084fc;
41
+ --accent-bg: rgba(192, 132, 252, 0.15);
42
+ --accent-border: rgba(192, 132, 252, 0.5);
43
+ --social-bg: rgba(47, 48, 58, 0.5);
44
+ --shadow:
45
+ rgba(0, 0, 0, 0.4) 0 10px 15px -3px, rgba(0, 0, 0, 0.25) 0 4px 6px -2px;
46
+ }
47
+
48
+ #social .button-icon {
49
+ filter: invert(1) brightness(2);
50
+ }
51
+ }
52
+
53
+ body {
54
+ margin: 0;
55
+ }
56
+
57
+ #root {
58
+ width: 1126px;
59
+ max-width: 100%;
60
+ margin: 0 auto;
61
+ text-align: center;
62
+ border-inline: 1px solid var(--border);
63
+ min-height: 100svh;
64
+ display: flex;
65
+ flex-direction: column;
66
+ box-sizing: border-box;
67
+ }
68
+
69
+ h1,
70
+ h2 {
71
+ font-family: var(--heading);
72
+ font-weight: 500;
73
+ color: var(--text-h);
74
+ }
75
+
76
+ h1 {
77
+ font-size: 56px;
78
+ letter-spacing: -1.68px;
79
+ margin: 32px 0;
80
+ @media (max-width: 1024px) {
81
+ font-size: 36px;
82
+ margin: 20px 0;
83
+ }
84
+ }
85
+ h2 {
86
+ font-size: 24px;
87
+ line-height: 118%;
88
+ letter-spacing: -0.24px;
89
+ margin: 0 0 8px;
90
+ @media (max-width: 1024px) {
91
+ font-size: 20px;
92
+ }
93
+ }
94
+ p {
95
+ margin: 0;
96
+ }
97
+
98
+ code,
99
+ .counter {
100
+ font-family: var(--mono);
101
+ display: inline-flex;
102
+ border-radius: 4px;
103
+ color: var(--text-h);
104
+ }
105
+
106
+ code {
107
+ font-size: 15px;
108
+ line-height: 135%;
109
+ padding: 4px 8px;
110
+ background: var(--code-bg);
111
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ // Core
2
+ export { SurveyEngine } from "./survey/core/SurveyEngine";
3
+ export { SurveyManager } from "./survey/core/SurveyManager";
4
+ export { LocalSurveyStorage } from "./survey/core/storage";
5
+
6
+ // Types
7
+ export type {
8
+ Survey,
9
+ SurveyIdentity,
10
+ } from "./survey/types";
@@ -0,0 +1,99 @@
1
+
2
+ import { describe, it, expect } from "vitest";
3
+
4
+ import { SurveyEngine } from "./SurveyEngine";
5
+ import { SurveyManager } from "./SurveyManager";
6
+ import { MemorySurveyStorage } from "./storage";
7
+
8
+ import type { Survey } from "../types";
9
+
10
+ const testSurvey: Survey = {
11
+ id: "SURVEY_001",
12
+ title: "Customer Feedback",
13
+ mode: "anonymous",
14
+ status: "active",
15
+ createdAt: new Date().toISOString(),
16
+
17
+ questions: [
18
+ {
19
+ id: "Q1",
20
+ title: "How was your experience?",
21
+ type: "rating",
22
+ required: true,
23
+ },
24
+ ],
25
+ };
26
+
27
+ describe("SurveyEngine", () => {
28
+
29
+ it("should display survey first time", () => {
30
+ const manager = new SurveyManager();
31
+ const storage = new MemorySurveyStorage();
32
+
33
+ manager.createSurvey(testSurvey);
34
+
35
+ const engine = new SurveyEngine(
36
+ manager,
37
+ storage
38
+ );
39
+
40
+ const identity = {
41
+ type: "anonymous" as const,
42
+ id: "visitor_101",
43
+ };
44
+
45
+ const survey = engine.getNextSurvey(identity);
46
+
47
+ expect(survey?.id).toBe("SURVEY_001");
48
+ });
49
+
50
+ it("should not display the same survey twice", () => {
51
+ const manager = new SurveyManager();
52
+ const storage = new MemorySurveyStorage();
53
+
54
+ manager.createSurvey(testSurvey);
55
+
56
+ const engine = new SurveyEngine(
57
+ manager,
58
+ storage
59
+ );
60
+
61
+ const identity = {
62
+ type: "anonymous" as const,
63
+ id: "visitor_101",
64
+ };
65
+
66
+ engine.getNextSurvey(identity);
67
+
68
+ const secondSurvey =
69
+ engine.getNextSurvey(identity);
70
+
71
+ expect(secondSurvey).toBeNull();
72
+ });
73
+
74
+ it("should allow a different visitor", () => {
75
+ const manager = new SurveyManager();
76
+ const storage = new MemorySurveyStorage();
77
+
78
+ manager.createSurvey(testSurvey);
79
+
80
+ const engine = new SurveyEngine(
81
+ manager,
82
+ storage
83
+ );
84
+
85
+ engine.getNextSurvey({
86
+ type: "anonymous",
87
+ id: "visitor_101",
88
+ });
89
+
90
+ const secondVisitor =
91
+ engine.getNextSurvey({
92
+ type: "anonymous",
93
+ id: "visitor_202",
94
+ });
95
+
96
+ expect(secondVisitor?.id).toBe("SURVEY_001");
97
+ });
98
+
99
+ });
@@ -0,0 +1,225 @@
1
+
2
+ import { nanoid } from "nanoid";
3
+
4
+ import type {
5
+ Survey,
6
+ SurveyIdentity,
7
+ SurveyResponse,
8
+ SurveyUser,
9
+ } from "../types";
10
+
11
+ import { SurveyManager } from "./SurveyManager";
12
+
13
+ import {
14
+ resolveIdentity,
15
+ } from "./identity";
16
+
17
+ import {
18
+ isSurveyEligible,
19
+ } from "./eligibility";
20
+
21
+ import {
22
+ getExecutions,
23
+ hasAlreadySeenSurvey,
24
+ recordSurveyExecution,
25
+ } from "./duplicate-check";
26
+
27
+ import {
28
+ LocalSurveyStorage,
29
+ type SurveyStorage,
30
+ } from "./storage";
31
+
32
+ const RESPONSE_KEY = "rls_survey_responses";
33
+
34
+ export class SurveyEngine {
35
+
36
+ private manager: SurveyManager;
37
+
38
+ private storage: SurveyStorage;
39
+
40
+ constructor(
41
+ manager: SurveyManager = new SurveyManager(),
42
+ storage: SurveyStorage = new LocalSurveyStorage()
43
+ ) {
44
+ this.manager = manager;
45
+ this.storage = storage;
46
+ }
47
+
48
+ /**
49
+ * Resolve current visitor identity.
50
+ */
51
+ getIdentity(
52
+ user?: SurveyUser
53
+ ): SurveyIdentity {
54
+
55
+ return resolveIdentity(
56
+ user,
57
+ this.storage
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Access survey manager.
63
+ */
64
+ getSurveyManager(): SurveyManager {
65
+ return this.manager;
66
+ }
67
+
68
+ /**
69
+ * Get next eligible survey.
70
+ *
71
+ * IMPORTANT:
72
+ * We record the display before returning
73
+ * the survey to the UI.
74
+ *
75
+ * This implements the "once shown" policy.
76
+ */
77
+ getNextSurvey(
78
+ identity: SurveyIdentity
79
+ ): Survey | null {
80
+
81
+ const activeSurveys =
82
+ this.manager.getActiveSurveys();
83
+
84
+ for (const survey of activeSurveys) {
85
+
86
+ const eligible = isSurveyEligible(
87
+ survey,
88
+ identity
89
+ );
90
+
91
+ if (!eligible) {
92
+ continue;
93
+ }
94
+
95
+ const alreadySeen =
96
+ hasAlreadySeenSurvey(
97
+ survey.id,
98
+ identity,
99
+ this.storage
100
+ );
101
+
102
+ if (alreadySeen) {
103
+ continue;
104
+ }
105
+
106
+ // Record before displaying the popup.
107
+ recordSurveyExecution(
108
+ survey.id,
109
+ identity,
110
+ this.storage
111
+ );
112
+
113
+ return survey;
114
+ }
115
+
116
+ return null;
117
+ }
118
+
119
+ /**
120
+ * Submit survey response.
121
+ */
122
+ submitSurvey(
123
+ surveyId: string,
124
+ identity: SurveyIdentity,
125
+ answers: Record<string, string | string[]>
126
+ ): SurveyResponse {
127
+
128
+ const survey =
129
+ this.manager.getSurveyById(surveyId);
130
+
131
+ if (!survey) {
132
+ throw new Error("Survey not found.");
133
+ }
134
+
135
+ const alreadyShown =
136
+ hasAlreadySeenSurvey(
137
+ surveyId,
138
+ identity,
139
+ this.storage
140
+ );
141
+
142
+ if (!alreadyShown) {
143
+ throw new Error(
144
+ "Survey must be displayed before submission."
145
+ );
146
+ }
147
+
148
+ // Validate required questions.
149
+ for (const question of survey.questions) {
150
+
151
+ if (!question.required) {
152
+ continue;
153
+ }
154
+
155
+ const answer = answers[question.id];
156
+
157
+ const isEmpty =
158
+ answer === undefined ||
159
+ (typeof answer === "string" && !answer.trim()) ||
160
+ (Array.isArray(answer) && answer.length === 0);
161
+
162
+ if (isEmpty) {
163
+ throw new Error(
164
+ `Please answer: ${question.title}`
165
+ );
166
+ }
167
+ }
168
+
169
+ const response: SurveyResponse = {
170
+ id: `response_${nanoid(12)}`,
171
+ surveyId,
172
+
173
+ identityType: identity.type,
174
+ identityId: identity.id,
175
+
176
+ answers,
177
+
178
+ submittedAt: new Date().toISOString(),
179
+ };
180
+
181
+ const existingResponses =
182
+ this.getResponses();
183
+
184
+ this.storage.setItem(
185
+ RESPONSE_KEY,
186
+ JSON.stringify([
187
+ ...existingResponses,
188
+ response,
189
+ ])
190
+ );
191
+
192
+ return response;
193
+ }
194
+
195
+ /**
196
+ * Get all responses.
197
+ */
198
+ getResponses(): SurveyResponse[] {
199
+
200
+ try {
201
+ const value =
202
+ this.storage.getItem(RESPONSE_KEY);
203
+
204
+ if (!value) {
205
+ return [];
206
+ }
207
+
208
+ const parsed: unknown = JSON.parse(value);
209
+
210
+ return Array.isArray(parsed)
211
+ ? parsed as SurveyResponse[]
212
+ : [];
213
+
214
+ } catch {
215
+ return [];
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Get execution history.
221
+ */
222
+ getExecutionHistory() {
223
+ return getExecutions(this.storage);
224
+ }
225
+ }
@@ -0,0 +1,82 @@
1
+
2
+ import type {
3
+ Survey,
4
+ } from "../types";
5
+
6
+ export class SurveyManager {
7
+
8
+ private surveys: Survey[] = [];
9
+
10
+ /**
11
+ * Add a new survey.
12
+ */
13
+ createSurvey(survey: Survey): void {
14
+
15
+ const exists = this.surveys.some(
16
+ (item) => item.id === survey.id
17
+ );
18
+
19
+ if (exists) {
20
+ throw new Error(
21
+ `Survey already exists: ${survey.id}`
22
+ );
23
+ }
24
+
25
+ this.surveys.push(survey);
26
+ }
27
+
28
+ /**
29
+ * Get all surveys.
30
+ */
31
+ getSurveys(): Survey[] {
32
+ return [...this.surveys];
33
+ }
34
+
35
+ /**
36
+ * Get survey by ID.
37
+ */
38
+ getSurveyById(
39
+ surveyId: string
40
+ ): Survey | undefined {
41
+
42
+ return this.surveys.find(
43
+ (survey) => survey.id === surveyId
44
+ );
45
+ }
46
+
47
+ /**
48
+ * Get active surveys.
49
+ */
50
+ getActiveSurveys(): Survey[] {
51
+
52
+ return this.surveys.filter(
53
+ (survey) => survey.status === "active"
54
+ );
55
+ }
56
+
57
+ /**
58
+ * Update survey.
59
+ */
60
+ updateSurvey(
61
+ surveyId: string,
62
+ updates: Partial<Survey>
63
+ ): void {
64
+
65
+ this.surveys = this.surveys.map(
66
+ (survey) =>
67
+ survey.id === surveyId
68
+ ? { ...survey, ...updates, id: surveyId }
69
+ : survey
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Delete survey.
75
+ */
76
+ deleteSurvey(surveyId: string): void {
77
+
78
+ this.surveys = this.surveys.filter(
79
+ (survey) => survey.id !== surveyId
80
+ );
81
+ }
82
+ }
@@ -0,0 +1,94 @@
1
+
2
+ import type {
3
+ SurveyExecution,
4
+ SurveyIdentity,
5
+ } from "../types";
6
+
7
+ import type {
8
+ SurveyStorage,
9
+ } from "./storage";
10
+
11
+ const EXECUTION_KEY = "rls_survey_executions";
12
+
13
+ /**
14
+ * Read survey execution history.
15
+ */
16
+ export function getExecutions(
17
+ storage: SurveyStorage
18
+ ): SurveyExecution[] {
19
+
20
+ try {
21
+ const value = storage.getItem(EXECUTION_KEY);
22
+
23
+ if (!value) {
24
+ return [];
25
+ }
26
+
27
+ const parsed: unknown = JSON.parse(value);
28
+
29
+ return Array.isArray(parsed)
30
+ ? parsed as SurveyExecution[]
31
+ : [];
32
+
33
+ } catch {
34
+ return [];
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Check if survey was already displayed
40
+ * to this identity.
41
+ */
42
+ export function hasAlreadySeenSurvey(
43
+ surveyId: string,
44
+ identity: SurveyIdentity,
45
+ storage: SurveyStorage
46
+ ): boolean {
47
+
48
+ const executions = getExecutions(storage);
49
+
50
+ return executions.some(
51
+ (execution) =>
52
+ execution.surveyId === surveyId &&
53
+ execution.identityType === identity.type &&
54
+ execution.identityId === identity.id
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Record survey display.
60
+ */
61
+ export function recordSurveyExecution(
62
+ surveyId: string,
63
+ identity: SurveyIdentity,
64
+ storage: SurveyStorage
65
+ ): void {
66
+
67
+ const executions = getExecutions(storage);
68
+
69
+ const alreadySeen = executions.some(
70
+ (execution) =>
71
+ execution.surveyId === surveyId &&
72
+ execution.identityType === identity.type &&
73
+ execution.identityId === identity.id
74
+ );
75
+
76
+ if (alreadySeen) {
77
+ return;
78
+ }
79
+
80
+ const execution: SurveyExecution = {
81
+ surveyId,
82
+ identityType: identity.type,
83
+ identityId: identity.id,
84
+ shownAt: new Date().toISOString(),
85
+ };
86
+
87
+ storage.setItem(
88
+ EXECUTION_KEY,
89
+ JSON.stringify([
90
+ ...executions,
91
+ execution,
92
+ ])
93
+ );
94
+ }
@@ -0,0 +1,35 @@
1
+
2
+ import type {
3
+ Survey,
4
+ SurveyIdentity,
5
+ } from "../types";
6
+
7
+ /**
8
+ * Check whether a survey matches
9
+ * the visitor's identity type.
10
+ */
11
+ export function isSurveyEligible(
12
+ survey: Survey,
13
+ identity: SurveyIdentity
14
+ ): boolean {
15
+
16
+ if (survey.status !== "active") {
17
+ return false;
18
+ }
19
+
20
+ if (
21
+ survey.mode === "anonymous" &&
22
+ identity.type !== "anonymous"
23
+ ) {
24
+ return false;
25
+ }
26
+
27
+ if (
28
+ survey.mode === "operational" &&
29
+ identity.type !== "authenticated"
30
+ ) {
31
+ return false;
32
+ }
33
+
34
+ return true;
35
+ }