-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.ts
More file actions
183 lines (171 loc) · 5.65 KB
/
users.ts
File metadata and controls
183 lines (171 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
IterableSuccessResponse,
IterableSuccessResponseSchema,
} from "../types/common.js";
import {
UserBulkUpdateListResponse,
UserBulkUpdateListResponseSchema,
} from "../types/lists.js";
import {
BulkUpdateUsersParams,
DeleteUserByEmailParams,
DeleteUserByUserIdParams,
GetSentMessagesParams,
GetSentMessagesResponse,
GetSentMessagesResponseSchema,
GetUserByEmailParams,
GetUserByIdParams,
GetUserFieldsResponse,
GetUserFieldsResponseSchema,
UpdateEmailParams,
UpdateUserParams,
UpdateUserSubscriptionsParams,
UserResponse,
UserResponseSchema,
} from "../types/users.js";
import type { BaseIterableClient, Constructor } from "./base.js";
/**
* User management operations mixin
*/
export function Users<T extends Constructor<BaseIterableClient>>(Base: T) {
return class extends Base {
/**
* Get a user by email address
*/
async getUserByEmail(
params: GetUserByEmailParams
): Promise<UserResponse> {
const response = await this.client.get(
`/api/users/${encodeURIComponent(params.email)}`
);
return this.validateResponse(response, UserResponseSchema);
}
/**
* Get a user by userId
*/
async getUserByUserId(
params: GetUserByIdParams
): Promise<UserResponse> {
const response = await this.client.get(
`/api/users/byUserId/${encodeURIComponent(params.userId)}`
);
return this.validateResponse(response, UserResponseSchema);
}
/**
* Update user data or add a user if none exists
* Accepts email OR userId in the userProfile parameter
*/
async updateUser(
userProfile: UpdateUserParams
): Promise<IterableSuccessResponse> {
const response = await this.client.post("/api/users/update", userProfile);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
/**
* Delete a user by email address
* Asynchronous operation - does not prevent future data collection
*/
async deleteUserByEmail(
params: DeleteUserByEmailParams
): Promise<IterableSuccessResponse> {
const response = await this.client.delete(
`/api/users/${encodeURIComponent(params.email)}`
);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
/**
* Delete a user by userId
* Asynchronous operation - does not prevent future data collection
* If multiple users share the same userId, they'll all be deleted
*/
async deleteUserByUserId(
params: DeleteUserByUserIdParams
): Promise<IterableSuccessResponse> {
const response = await this.client.delete(
`/api/users/byUserId/${encodeURIComponent(params.userId)}`
);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
/**
* Update a user's email address
* Only use with email-based projects. For userId/hybrid projects, use updateUser instead.
* Returns an error if the new email already exists or has been forgotten via GDPR.
*/
async updateEmail(
params: UpdateEmailParams
): Promise<IterableSuccessResponse> {
const response = await this.client.post("/api/users/updateEmail", params);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
/**
* Update user subscriptions
* IMPORTANT: This endpoint overwrites (does not merge) existing data for any non-null fields specified.
*/
async updateUserSubscriptions(
params: UpdateUserSubscriptionsParams
): Promise<IterableSuccessResponse> {
const response = await this.client.post(
"/api/users/updateSubscriptions",
params
);
return this.validateResponse(response, IterableSuccessResponseSchema);
}
/**
* Bulk update user data
*/
async bulkUpdateUsers(
params: BulkUpdateUsersParams
): Promise<UserBulkUpdateListResponse> {
const response = await this.client.post("/api/users/bulkUpdate", params);
return this.validateResponse(response, UserBulkUpdateListResponseSchema);
}
/**
* Get messages sent to a user
*/
async getSentMessages(
params: GetSentMessagesParams
): Promise<GetSentMessagesResponse> {
const queryParams = new URLSearchParams();
if (params.email) {
queryParams.append("email", params.email);
}
if (params.userId) {
queryParams.append("userId", params.userId);
}
if (params.limit !== undefined) {
queryParams.append("limit", params.limit.toString());
}
if (params.campaignIds && params.campaignIds.length > 0) {
params.campaignIds.forEach((id) =>
queryParams.append("campaignIds", id.toString())
);
}
if (params.startDateTime) {
queryParams.append("startDateTime", params.startDateTime);
}
if (params.endDateTime) {
queryParams.append("endDateTime", params.endDateTime);
}
if (params.excludeBlastCampaigns !== undefined) {
queryParams.append(
"excludeBlastCampaigns",
params.excludeBlastCampaigns.toString()
);
}
if (params.messageMedium) {
queryParams.append("messageMedium", params.messageMedium);
}
const response = await this.client.get(
`/api/users/getSentMessages?${queryParams.toString()}`
);
return this.validateResponse(response, GetSentMessagesResponseSchema);
}
/**
* Get all user profile field definitions
*/
async getUserFields(): Promise<GetUserFieldsResponse> {
const response = await this.client.get("/api/users/getFields");
return this.validateResponse(response, GetUserFieldsResponseSchema);
}
};
}