@mbc-cqrs-serverless/survey-template
Version:
170 lines • 7.84 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var SurveyTemplateService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SurveyTemplateService = void 0;
const core_1 = require("@mbc-cqrs-serverless/core");
const common_1 = require("@nestjs/common");
const path_1 = require("path");
const ulid_1 = require("ulid");
const survey_template_command_dto_1 = require("./dto/survey-template-command.dto");
const survey_template_data_entity_1 = require("./entity/survey-template-data.entity");
const survey_template_data_list_entity_1 = require("./entity/survey-template-data-list.entity");
const keys_1 = require("./keys");
const survey_template_module_definition_1 = require("./survey-template.module-definition");
const utils_1 = require("./utils");
let SurveyTemplateService = SurveyTemplateService_1 = class SurveyTemplateService {
constructor(prismaService, commandService, dataService) {
this.prismaService = prismaService;
this.commandService = commandService;
this.dataService = dataService;
this.logger = new common_1.Logger(SurveyTemplateService_1.name);
}
async searchData(tenantCode, searchDto) {
const where = {
isDeleted: false,
tenantCode,
};
if (searchDto.keyword?.trim()) {
where.OR = [
{ name: { contains: searchDto.keyword.trim(), mode: 'insensitive' } },
{
description: {
contains: searchDto.keyword.trim(),
mode: 'insensitive',
},
},
];
}
if (searchDto.id) {
where.id = searchDto.id;
}
if (searchDto.pk) {
where.pk = searchDto.pk;
}
if (searchDto.sk) {
where.sk = searchDto.sk;
}
const { pageSize = 10, page = 1, orderBys = ['-createdAt'] } = searchDto;
const [total, items] = await Promise.all([
this.prismaService.surveyTemplate.count({ where }),
this.prismaService.surveyTemplate.findMany({
where,
take: pageSize,
skip: pageSize * (page - 1),
orderBy: (0, utils_1.getOrderBys)(orderBys),
}),
]);
return new survey_template_data_list_entity_1.SurveyTemplateDataListEntity({
total,
items: items.map((item) => new survey_template_data_entity_1.SurveyTemplateDataEntity({
...item,
attributes: {
surveyTemplate: item.surveyTemplate,
description: item.description,
},
})),
});
}
async create(createDto, options) {
const { tenantCode } = (0, core_1.getUserContext)(options.invokeContext);
const pk = `SURVEY${core_1.KEY_SEPARATOR}${tenantCode}`;
const sk = `${keys_1.SURVEY_TEMPLATE_SK_PREFIX}${core_1.KEY_SEPARATOR}${(0, ulid_1.ulid)()}`;
const commandDto = {
pk,
sk,
id: (0, core_1.generateId)(pk, sk),
tenantCode,
code: sk,
type: keys_1.SURVEY_TEMPLATE_SK_PREFIX,
version: core_1.VERSION_FIRST,
name: createDto.name,
attributes: {
...createDto.attributes,
description: '',
additionalProperties: undefined,
},
};
this.logger.debug('commandService:' + this.commandService.tableName);
const item = await this.commandService.publishAsync(commandDto, {
invokeContext: options.invokeContext,
source: (0, core_1.getCommandSource)((0, path_1.basename)(__dirname), this.constructor.name, 'create'),
});
return new survey_template_data_entity_1.SurveyTemplateDataEntity(item);
}
async findOne(detailDto) {
const item = await this.dataService.getItem(detailDto);
if (!item) {
throw new common_1.NotFoundException('Survey template not found!');
}
return new survey_template_data_entity_1.SurveyTemplateDataEntity(item);
}
async update(detailDto, updateDto, options) {
const userContext = (0, core_1.getUserContext)(options.invokeContext);
const { tenantCode } = (0, utils_1.parsePk)(detailDto.pk);
if (userContext.tenantCode !== tenantCode) {
throw new common_1.BadRequestException('Invalid tenant code');
}
const data = (await this.dataService.getItem(detailDto));
if (!data) {
throw new common_1.NotFoundException('Survey template not found!');
}
const commandDto = new survey_template_command_dto_1.SurveyTemplateCommandDto({
pk: data.pk,
sk: data.sk,
version: data.version,
name: updateDto.name ?? data.name,
isDeleted: updateDto.isDeleted ?? data.isDeleted,
attributes: {
...data.attributes,
...updateDto.attributes,
},
});
const item = await this.commandService.publishPartialUpdateAsync(commandDto, {
invokeContext: options.invokeContext,
source: (0, core_1.getCommandSource)((0, path_1.basename)(__dirname), this.constructor.name, 'update'),
});
return new survey_template_data_entity_1.SurveyTemplateDataEntity(item);
}
async remove(detailDto, options) {
const userContext = (0, core_1.getUserContext)(options.invokeContext);
const { tenantCode } = (0, utils_1.parsePk)(detailDto.pk);
if (userContext.tenantCode !== tenantCode) {
throw new common_1.BadRequestException('Invalid tenant code');
}
const data = (await this.dataService.getItem(detailDto));
if (!data) {
throw new common_1.NotFoundException('Survey template not found!');
}
const commandDto = new survey_template_command_dto_1.SurveyTemplateCommandDto({
pk: data.pk,
sk: data.sk,
version: data.version,
isDeleted: true,
});
const item = await this.commandService.publishPartialUpdateAsync(commandDto, {
invokeContext: options.invokeContext,
source: (0, core_1.getCommandSource)((0, path_1.basename)(__dirname), this.constructor.name, 'remove'),
});
return new survey_template_data_entity_1.SurveyTemplateDataEntity(item);
}
};
exports.SurveyTemplateService = SurveyTemplateService;
exports.SurveyTemplateService = SurveyTemplateService = SurveyTemplateService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(survey_template_module_definition_1.PRISMA_SERVICE)),
__metadata("design:paramtypes", [Object, core_1.CommandService,
core_1.DataService])
], SurveyTemplateService);
//# sourceMappingURL=survey-template.service.js.map