Skip to content
Open
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
3 changes: 3 additions & 0 deletions server/src/models/surveyMeta.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export class SurveyMeta extends BaseEntity {
@Column()
isDeleted: boolean;

@Column()
isCompletelyDeleted: boolean;

@Column()
deletedAt: Date;

Expand Down
64 changes: 64 additions & 0 deletions server/src/modules/survey/__test/survey.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,68 @@ describe('SurveyController', () => {
expect(result.code).toBe(200);
});
});

describe('restoreSurvey', () => {
it('should restore a survey successfully', async () => {
const surveyId = new ObjectId();
const surveyMeta = {
_id: surveyId,
surveyPath: 'test/path',
};

jest
.spyOn(surveyMetaService, 'restoreSurveyMeta')
.mockResolvedValue({ success: true });
jest
.spyOn(responseSchemaService, 'restoreResponseSchema')
.mockResolvedValue({ success: true });

const result = await controller.restoreSurvey({
surveyMeta,
user: { username: 'testUser', _id: new ObjectId() },
});

expect(result).toEqual({ code: 200 });
expect(surveyMetaService.restoreSurveyMeta).toHaveBeenCalledWith({
surveyId: surveyId.toString(),
operator: 'testUser',
operatorId: expect.any(String),
});
expect(responseSchemaService.restoreResponseSchema).toHaveBeenCalledWith({
surveyPath: 'test/path',
});
});
});

describe('completelyDeleteSurvey', () => {
it('should completely delete a survey successfully', async () => {
const surveyId = new ObjectId();
const surveyMeta = {
_id: surveyId,
surveyPath: 'test/path',
};

jest
.spyOn(surveyMetaService, 'completelyDeleteSurveyMeta')
.mockResolvedValue({ success: true });
jest
.spyOn(responseSchemaService, 'completelyDeleteResponseSchema')
.mockResolvedValue({ success: true });

const result = await controller.completelyDeleteSurvey({
surveyMeta,
user: { username: 'testUser', _id: new ObjectId() },
});

expect(result).toEqual({ code: 200 });
expect(surveyMetaService.completelyDeleteSurveyMeta).toHaveBeenCalledWith({
surveyId: surveyId.toString(),
operator: 'testUser',
operatorId: expect.any(String),
});
expect(responseSchemaService.completelyDeleteResponseSchema).toHaveBeenCalledWith({
surveyPath: 'test/path',
});
});
});
});
79 changes: 79 additions & 0 deletions server/src/modules/survey/__test/surveyMeta.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,83 @@ describe('SurveyMetaService', () => {
});
});
});

describe('restoreSurveyMeta', () => {
it('should restore a survey and update its status', async () => {
const surveyId = new ObjectId().toString();
const operator = 'restorer';
const operatorId = 'restorerId';

const mockUpdateResult = {
matchedCount: 1,
modifiedCount: 1,
acknowledged: true,
};

jest.spyOn(surveyRepository, 'updateOne').mockResolvedValue(mockUpdateResult);

const result = await service.restoreSurveyMeta({
surveyId,
operator,
operatorId,
});

expect(surveyRepository.updateOne).toHaveBeenCalledWith(
{ _id: new ObjectId(surveyId) },
{
$set: {
isDeleted: false,
operator,
operatorId,
deletedAt: null,
curStatus: {
status: RECORD_STATUS.NEW,
date: expect.any(Number),
},
subStatus: {
status: RECORD_SUB_STATUS.DEFAULT,
date: expect.any(Number),
},
},
},
);
expect(result).toEqual(mockUpdateResult);
});
});

describe('completelyDeleteSurveyMeta', () => {
it('should completely delete a survey', async () => {
const surveyId = new ObjectId().toString();
const operator = 'deleter';
const operatorId = 'deleterId';

const mockUpdateResult = {
matchedCount: 1,
modifiedCount: 1,
acknowledged: true,
};

jest.spyOn(surveyRepository, 'updateOne').mockResolvedValue(mockUpdateResult);

const result = await service.completelyDeleteSurveyMeta({
surveyId,
operator,
operatorId,
});

expect(surveyRepository.updateOne).toHaveBeenCalledWith(
{ _id: new ObjectId(surveyId) },
{
$set: {
isDeleted: true,
isCompletelyDeleted: true,
operator,
operatorId,
deletedAt: expect.any(Date),
},
},
);
expect(result).toEqual(mockUpdateResult);
});
});
});
54 changes: 54 additions & 0 deletions server/src/modules/survey/controllers/survey.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,60 @@ export class SurveyController {
};
}

@HttpCode(200)
@Post('/restoreSurvey')
@UseGuards(SurveyGuard)
@SetMetadata('surveyId', 'body.surveyId')
@SetMetadata('surveyPermission', [SURVEY_PERMISSION.SURVEY_CONF_MANAGE])
@UseGuards(Authentication)
async restoreSurvey(@Request() req) {
const surveyMeta = req.surveyMeta;

const resMetaRes = await this.surveyMetaService.restoreSurveyMeta({
surveyId: surveyMeta._id.toString(),
operator: req.user.username,
operatorId: req.user._id.toString(),
});
const resResponseRes =
await this.responseSchemaService.restoreResponseSchema({
surveyPath: surveyMeta.surveyPath,
});

this.logger.info(JSON.stringify(resMetaRes));
this.logger.info(JSON.stringify(resResponseRes));

return {
code: 200,
};
}

@HttpCode(200)
@Post('/completelyDeleteSurvey')
@UseGuards(SurveyGuard)
@SetMetadata('surveyId', 'body.surveyId')
@SetMetadata('surveyPermission', [SURVEY_PERMISSION.SURVEY_CONF_MANAGE])
@UseGuards(Authentication)
async completelyDeleteSurvey(@Request() req) {
const surveyMeta = req.surveyMeta;

const completelyDelMetaRes = await this.surveyMetaService.completelyDeleteSurveyMeta({
surveyId: surveyMeta._id.toString(),
operator: req.user.username,
operatorId: req.user._id.toString(),
});
const completelyDelResponseRes =
await this.responseSchemaService.completelyDeleteResponseSchema({
surveyPath: surveyMeta.surveyPath,
});

this.logger.info(JSON.stringify(completelyDelMetaRes));
this.logger.info(JSON.stringify(completelyDelResponseRes));

return {
code: 200,
};
}

@HttpCode(200)
@Post('/pausingSurvey')
@UseGuards(SurveyGuard)
Expand Down
86 changes: 84 additions & 2 deletions server/src/modules/survey/controllers/surveyMeta.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
UseGuards,
Request,
SetMetadata,
Injectable,

Check failure on line 11 in server/src/modules/survey/controllers/surveyMeta.controller.ts

View workflow job for this annotation

GitHub Actions / Lint

'Injectable' is defined but never used
} from '@nestjs/common';
import * as Joi from 'joi';
import moment from 'moment';
import { ApiTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { MongoRepository } from 'typeorm';
import { SurveyMeta } from 'src/models/surveyMeta.entity';
import { ObjectId } from 'mongodb';

import { SurveyMetaService } from '../services/surveyMeta.service';
import { WorkspaceService } from '../../workspace/services/workspace.service';

import { getFilter, getOrder } from 'src/utils/surveyUtil';
import { HttpException } from 'src/exceptions/httpException';
Expand All @@ -36,6 +42,9 @@
private readonly surveyMetaService: SurveyMetaService,
private readonly logger: Logger,
private readonly collaboratorService: CollaboratorService,
private readonly workspaceService: WorkspaceService,
@InjectRepository(SurveyMeta)
private surveyMetaRepository: MongoRepository<SurveyMeta>,
) {}

@Post('/updateMeta')
Expand Down Expand Up @@ -90,7 +99,7 @@
this.logger.error(error.message);
throw new HttpException('参数有误', EXCEPTION_CODE.PARAMETER_ERROR);
}
const { curPage, pageSize, workspaceId, groupId } = value;
const { curPage, pageSize, workspaceId, groupId ,recycleId} = value;
let filter = {},
order = {};
if (value.filter) {
Expand All @@ -109,15 +118,25 @@
}
const userId = req.user._id.toString();
let cooperationList = [];
let workspaceList = [];
if (groupId === GROUP_STATE.ALL) {
cooperationList =
await this.collaboratorService.getCollaboratorListByUserId({ userId });
}
if (value.recycleId) {
cooperationList =
await this.collaboratorService.getCollaboratorListByUserId({ userId });
const getWorkspaceListResult =
await this.workspaceService.getWorkspaceListByUserId(userId);
workspaceList = getWorkspaceListResult.surveys;
}
const cooperSurveyIdMap = cooperationList.reduce((pre, cur) => {
pre[cur.surveyId] = cur;
return pre;
}, {});
const surveyIdList = cooperationList.map((item) => item.surveyId);
const surveyIdList1 = cooperationList.map((item) => item.surveyId);
const surveyIdList2 = workspaceList.map((item) => item._id.toString());
const surveyIdList = [...surveyIdList1, ...surveyIdList2];
const username = req.user.username;
const data = await this.surveyMetaService.getSurveyMetaList({
pageNum: curPage,
Expand All @@ -129,6 +148,7 @@
workspaceId,
groupId,
surveyIdList,
recycleId
});
return {
code: 200,
Expand All @@ -143,6 +163,7 @@
item.curStatus.date = moment(item.curStatus.date).format(fmt);
item.subStatus.date = moment(item.subStatus.date).format(fmt);
item.updatedAt = moment(item.updatedAt).format(fmt);
item.deletedAt = moment(item.deletedAt).format(fmt);
const surveyId = item._id.toString();
if (cooperSurveyIdMap[surveyId]) {
item.isCollaborated = true;
Expand All @@ -157,4 +178,65 @@
},
};
}

@Get('/getRecycleTotal')
@HttpCode(200)
@UseGuards(Authentication)
async getRecycleTotal(@Request() req) {
const userId = req.user._id.toString();
let cooperationList = [];
let workspaceList = [];
cooperationList =
await this.collaboratorService.getCollaboratorListByUserId({ userId });
const getWorkspaceListResult =
await this.workspaceService.getWorkspaceListByUserId(userId);
workspaceList = getWorkspaceListResult.surveys;
const surveyIdList1 = cooperationList.map((item) => item.surveyId);
const surveyIdList2 = workspaceList.map((item) => item._id.toString());
const surveyIdList = [...surveyIdList1, ...surveyIdList2];

Check failure on line 196 in server/src/modules/survey/controllers/surveyMeta.controller.ts

View workflow job for this annotation

GitHub Actions / Lint

'surveyIdList' is assigned a value but never used

const surveys = await this.surveyMetaRepository.find({
where: {
_id: {
$in: surveyIdList1.map(id => new ObjectId(id))
},
isDeleted: true,
isCompletelyDeleted: { $ne: true }
}
});
const surveytotal1 = surveys.length; // 直接获取数组长度

const surveys2 = await this.surveyMetaRepository.find({
where: {
_id: {
$in: surveyIdList2.map(id => new ObjectId(id))
},
isDeleted: true,
isCompletelyDeleted: { $ne: true }
}
});
const surveytotal2 = surveys2.length;
const surveys3 = await this.surveyMetaRepository.find({
where: {
ownerId: userId,
isDeleted: true,
isCompletelyDeleted: { $ne: true },
$and: [
{
workspaceId: { $exists: false },
},
{
workspaceId: null,
},
],
}
});
const surveytotal3 = surveys3.length;
return {
code: 200,
total: surveytotal1 + surveytotal2 + surveytotal3
};
}
}


4 changes: 4 additions & 0 deletions server/src/modules/survey/dto/getSurveyMetaList.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class GetSurveyListDto {
@ApiProperty({ description: '分组id', required: false })
groupId?: string;

@ApiProperty({ description: '是否查询回收站', required: false })
recycleId?: boolean;

static validate(data) {
return Joi.object({
curPage: Joi.number().required(),
Expand All @@ -28,6 +31,7 @@ export class GetSurveyListDto {
order: Joi.string().allow(null),
workspaceId: Joi.string().allow(null, ''),
groupId: Joi.string().allow(null, ''),
recycleId: Joi.boolean().allow(null, false).default(false),
}).validate(data);
}
}
Loading
Loading