Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JsonController, Get, Delete, Authorized, Post, Req, Body, QueryParams,
import { SegmentService, SegmentWithStatus } from '../services/SegmentService';
import { Segment } from '../models/Segment';
import { AppRequest, PaginationResponse } from '../../types';
import { SEGMENT_STATUS } from 'upgrade_types';
import { IImportError, SEGMENT_STATUS } from 'upgrade_types';
import {
DeleteListInputValidator,
IdValidator,
Expand All @@ -12,6 +12,7 @@ import {
SegmentIdValidator,
SegmentImportError,
SegmentInputValidator,
SegmentListImportValidation,
} from './validators/SegmentInputValidator';
import { SegmentPaginatedParamsValidator } from './validators/SegmentPaginatedParamsValidator';
import { ExperimentSegmentInclusion } from '../models/ExperimentSegmentInclusion';
Expand Down Expand Up @@ -622,6 +623,41 @@ export class SegmentController {
return this.segmentService.importSegments(segments, request.logger);
}

/**
* @swagger
* /segments/list/import:
* post:
* description: Import a list to a parent segment
* tags:
* - Segment
* produces:
* - application/json
* parameters:
* - in: body
* name: segment
* description: Segment list object
* required: true
* schema:
* type: object
* $ref: '#/definitions/Segment'
* responses:
* '200':
* description: Import a list to a parent segment
* schema:
* $ref: '#/definitions/segmentResponse'
* '401':
* description: Authorization Required Error
* '500':
* description: Internal Server Error, Insert Error in database, SegmentId is not valid, JSON format is not valid
*/
@Post('/list/import')
public importLists(
@Body({ validate: true }) lists: SegmentListImportValidation,
@Req() request: AppRequest
): Promise<IImportError[]> {
return this.segmentService.importLists(lists, request.logger);
}

/**
* @swagger
* /segments/validation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,14 @@ export class SegmentIdValidator {
@IsUUID()
public segmentId: string;
}

export class SegmentListImportValidation {
@IsArray()
@ValidateNested({ each: true })
@Type(() => SegmentFile)
public files: SegmentFile[];

@IsUUID()
@IsNotEmpty()
public parentSegmentId: string;
}
15 changes: 15 additions & 0 deletions backend/packages/Upgrade/src/api/services/SegmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
Group,
SegmentValidationObj,
ListInputValidator,
SegmentListImportValidation,
} from '../controllers/validators/SegmentInputValidator';
import { ExperimentSegmentExclusionRepository } from '../repositories/ExperimentSegmentExclusionRepository';
import { ExperimentSegmentInclusionRepository } from '../repositories/ExperimentSegmentInclusionRepository';
Expand Down Expand Up @@ -518,6 +519,20 @@ export class SegmentService {
return validatedSegments.importErrors;
}

public async importLists(lists: SegmentListImportValidation, logger: UpgradeLogger): Promise<any> {
const validatedLists = await this.checkSegmentsValidity(lists.files);

for (const list of validatedLists.segments) {
// Giving new id to avoid segment duplication
list.id = uuid();
list.type = SEGMENT_TYPE.PRIVATE;

logger.info({ message: `Import segment list => ${JSON.stringify(list, undefined, 2)}` });
await this.addList({ ...list, parentSegmentId: lists.parentSegmentId }, logger);
}
return validatedLists.importErrors;
}

public async checkSegmentsValidity(fileData: SegmentFile[]): Promise<SegmentValidationObj> {
const importFileErrors: SegmentImportError[] = [];
const segments = fileData.filter((segment) => path.extname(segment.fileName) === '.json');
Expand Down
15 changes: 15 additions & 0 deletions backend/packages/Upgrade/test/unit/services/SegmentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,21 @@ describe('Segment Service Testing', () => {
expect(segments).toEqual(returnSegment);
});

it('should import a segment list', async () => {
const returnSegment = [
{
fileName: 'seg1.json',
error: null,
compatibilityType: IMPORT_COMPATIBILITY_TYPE.COMPATIBLE,
},
];
service.getSegmentByIds = jest.fn().mockResolvedValue([seg1, seg2]);
repo.find = jest.fn().mockResolvedValue([]);
service.addList = jest.fn().mockResolvedValue(segValSegment);
const segments = await service.importLists({ parentSegmentId: 'seg1', files: [segValImportFile] }, logger);
expect(segments).toEqual(returnSegment);
});

it('should throw an error when trying to import a segment that includes an unknown subsegment', async () => {
const returnSegment = [
{
Expand Down
Loading