Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | 1x 1x 1x 1x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 30x 30x 30x 30x 49x 9x 9x 8x 8x 1x 1x 3x 9x 9x 49x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 2x 2x 2x 2x 3x 1x 1x 1x 1x 1x 3x 6x 6x 6x 6x 49x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 3x 3x 3x 3x 4x 1x 1x 1x 1x 1x 4x 5x 5x 5x 5x 49x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 49x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 3x 49x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 3x 49x 49x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 49x 6x 1x 1x 6x 1x 1x 6x 6x 1x 1x 6x 49x 5x 5x 1x 1x 5x 5x 1x 1x 1x 5x 49x 1x 1x | import { NotFoundError, ValidationError } from '../../api/errorHandling';
import { newsletterSourceGroupApi } from '../../api/newsletterSourceGroupApi';
import { NewsletterSourceGroup } from '../../types';
import { BaseService } from '../base/BaseService';
interface NewsletterSourceGroupOperationResult {
success: boolean;
group?: NewsletterSourceGroup;
error?: string;
}
interface NewsletterSourceGroupServiceOptions {
enableOptimisticUpdates?: boolean;
batchSize?: number;
}
interface CreateNewsletterSourceGroupParams {
name: string;
sourceIds: string[];
}
interface UpdateNewsletterSourceGroupParams {
name?: string;
sourceIds?: string[];
}
export class NewsletterSourceGroupService extends BaseService {
private groupOptions: NewsletterSourceGroupServiceOptions;
constructor(options: NewsletterSourceGroupServiceOptions = {}) {
super({
retryOptions: {
maxRetries: 3,
baseDelay: 1000,
},
timeout: 30000,
});
this.groupOptions = {
enableOptimisticUpdates: true,
batchSize: 50,
...options,
};
}
/**
* Get all newsletter source groups
*/
async getGroups(): Promise<NewsletterSourceGroup[]> {
return this.withRetry(async () => {
return await newsletterSourceGroupApi.getAll();
}, 'getGroups');
}
/**
* Get a single newsletter source group by ID
*/
async getGroup(id: string): Promise<NewsletterSourceGroup | null> {
this.validateString(id, 'Group ID')
return this.withRetry(async () => {
const group = await newsletterSourceGroupApi.getById(id);
if (!group) {
throw new NotFoundError(`Newsletter source group with ID ${id} not found`);
}
return group;
}, 'getGroup');
}
/**
* Create a new newsletter source group
*/
async createGroup(
params: CreateNewsletterSourceGroupParams
): Promise<NewsletterSourceGroupOperationResult> {
this.validateCreateParams(params);
return this.executeWithLogging(
async () => {
try {
const group = await this.withRetry(
() => newsletterSourceGroupApi.create(params),
'createGroup'
);
return {
success: true,
group,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
},
'createGroup',
{ params }
);
}
/**
* Update an existing newsletter source group
*/
async updateGroup(
id: string,
updates: UpdateNewsletterSourceGroupParams
): Promise<NewsletterSourceGroupOperationResult> {
this.validateString(id, 'group ID');
this.validateUpdateParams(updates);
return this.executeWithLogging(
async () => {
try {
const group = await this.withRetry(
() => newsletterSourceGroupApi.update({ id, name: updates.name || '', sourceIds: updates.sourceIds || [] }),
'updateGroup'
);
return {
success: true,
group,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
},
'updateGroup',
{ id, updates }
);
}
/**
* Delete a newsletter source group
*/
async deleteGroup(id: string): Promise<NewsletterSourceGroupOperationResult> {
this.validateString(id, 'group ID');
return this.executeWithLogging(
async () => {
try {
await this.withRetry(
() => newsletterSourceGroupApi.delete(id),
'deleteGroup'
);
return {
success: true,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
},
'deleteGroup',
{ id }
);
}
/**
* Add sources to a group
*/
async addSourcesToGroup(
groupId: string,
sourceIds: string[]
): Promise<NewsletterSourceGroupOperationResult> {
this.validateString(groupId, 'group ID');
if (!Array.isArray(sourceIds) || sourceIds.length === 0) {
throw new ValidationError('Source IDs array cannot be empty');
}
return this.executeWithLogging(
async () => {
try {
// First add the sources
await this.withRetry(
() => newsletterSourceGroupApi.addSources({ groupId, sourceIds }),
'addSourcesToGroup'
);
// Then fetch the updated group
const updatedGroup = await this.getGroup(groupId);
if (!updatedGroup) {
throw new Error('Failed to fetch updated group after adding sources');
}
return {
success: true,
group: updatedGroup,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
},
'addSourcesToGroup',
{ groupId, sourceIds }
);
}
/**
* Remove sources from a group
*/
async removeSourcesFromGroup(
groupId: string,
sourceIds: string[]
): Promise<NewsletterSourceGroupOperationResult> {
this.validateString(groupId, 'group ID');
if (!Array.isArray(sourceIds) || sourceIds.length === 0) {
throw new ValidationError('Source IDs array cannot be empty');
}
return this.executeWithLogging(
async () => {
try {
// First remove the sources
const success = await this.withRetry(
() => newsletterSourceGroupApi.removeSources({ groupId, sourceIds }),
'removeSourcesFromGroup'
);
if (!success) {
throw new Error('Failed to remove sources from group');
}
// Then fetch the updated group
const updatedGroup = await this.getGroup(groupId);
if (!updatedGroup) {
throw new Error('Failed to fetch updated group after removing sources');
}
return {
success: true,
group: updatedGroup,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
},
'removeSourcesFromGroup',
{ groupId, sourceIds }
);
}
/**
* Find the group that contains a given sourceId
*/
findGroupBySourceId(groups: NewsletterSourceGroup[], sourceId: string): NewsletterSourceGroup | null {
for (const group of groups) {
if (group.sources && group.sources.some(source => source.id === sourceId)) {
return group;
}
}
return null;
}
// /**
// * Delete multiple newsletter source groups
// */
// async bulkDelete(ids: string[]): Promise<BulkNewsletterSourceGroupOperationResult> {
// if (!Array.isArray(ids) || ids.length === 0) {
// throw new ValidationError('IDs array cannot be empty');
// }
// return this.executeWithLogging(
// async () => {
// try {
// const result = await this.withRetry(
// () => newsletterSourceGroupApi.bulkDelete(ids),
// 'bulkDelete'
// );
// return {
// success: true,
// failedIds: result.failed,
// };
// } catch (error) {
// return {
// success: false,
// error: error instanceof Error ? error.message : 'Unknown error',
// failedIds: ids,
// };
// }
// },
// 'bulkDelete',
// { idsCount: ids.length }
// );
// }
/**
* Get groups statistics
*/
async getGroupsStats(): Promise<{
total: number;
totalSources: number;
averageSourcesPerGroup: number;
}> {
return this.withRetry(async () => {
const groups = await newsletterSourceGroupApi.getAll();
const totalSources = groups.reduce((sum, group) => sum + (group.sources?.length || 0), 0);
return {
total: groups.length,
totalSources,
averageSourcesPerGroup: groups.length > 0 ? Math.round(totalSources / groups.length * 100) / 100 : 0,
};
}, 'getGroupsStats');
}
/**
* Validate create group parameters
*/
private validateCreateParams(params: CreateNewsletterSourceGroupParams): void {
if (!params.name || typeof params.name !== 'string') {
throw new ValidationError('Group name is required');
}
if (params.name.length < 2 || params.name.length > 100) {
throw new ValidationError('Group name must be between 2 and 100 characters');
}
if (!Array.isArray(params.sourceIds)) {
throw new ValidationError('Source IDs must be an array');
}
// Allow empty sourceIds array for creating empty groups
if (params.sourceIds.some(id => typeof id !== 'string' || !id.trim())) {
throw new ValidationError('All source IDs must be non-empty strings');
}
}
/**
* Validate update group parameters
*/
private validateUpdateParams(params: UpdateNewsletterSourceGroupParams): void {
if (params.name !== undefined) {
if (typeof params.name !== 'string' || params.name.length < 2 || params.name.length > 100) {
throw new ValidationError('Group name must be between 2 and 100 characters');
}
}
if (params.sourceIds !== undefined) {
if (!Array.isArray(params.sourceIds)) {
throw new ValidationError('Source IDs must be an array');
}
if (params.sourceIds.some(id => typeof id !== 'string' || !id.trim())) {
throw new ValidationError('All source IDs must be non-empty strings');
}
}
}
}
/**
* Standalone helper to find the group containing a sourceId
*/
export function findGroupBySourceId(groups: NewsletterSourceGroup[], sourceId: string): NewsletterSourceGroup | null {
for (const group of groups) {
if (group.sources && group.sources.some(source => source.id === sourceId)) {
return group;
}
}
return null;
}
// Export singleton instance
export const newsletterSourceGroupService = new NewsletterSourceGroupService();
|