芝麻web文件管理V1.00
编辑当前文件:/home/freeclou/app.optimyar.com/backend/api/discount/services/discount.js
'use strict'; /** * Read the documentation (https://strapi.io/documentation/v3.x/concepts/services.html#core-services) * to customize this service */ module.exports = { getClientKey(data) { let {amount, code} = data; amount = amount / 65; return code.substr(0, 2) + amount.toString() + strapi.services.helpers.reverseString(code.substr(2)); }, async isValid(data) { const expectedKey = this.getClientKey(data); //other validations return expectedKey === data.key; }, async create(data, {files} = {}) { data.key = strapi.services.helpers.reverseString(data.key); const entry = await strapi.query('discount').create(data); // if (files) { // // automatically uploads the files based on the entry and the model // await strapi.entityService.uploadFiles(entry, files, { // model: 'restaurant', // // if you are using a plugin's model you will have to add the `plugin` key (plugin: 'users-permissions') // }); // return this.findOne({ id: entry.id }); // } return entry; }, async verifyGetByCode(code) { const letterNumber = /^[0-9a-zA-Z_]+$/; if (code && !code.match(letterNumber)) { return {result: false, entity: "buyCourse.errors.invalidDiscount"}; } //always verify result is a object : {result:boolean , message:any} const entity = await strapi.services.discount.findOne({code}); if (entity == null) { return {result: false, entity: "buyCourse.errors.invalidDiscount"}; } return this.verifyDiscountForUse(entity) }, async verifyDiscountForUse(entity) { if (entity == null || !entity.isEnabled) { return {result: false, entity: "buyCourse.errors.invalidDiscount"}; } if (!strapi.services.helpers.isNowBetweenDates(entity.fromDate, entity.toDate)) { return {result: false, entity: "buyCourse.errors.expiredDiscount"}; } return {result: true, entity}; }, async verifyGetByCodeAndCourse(code, levelId) { const byCode = await this.verifyGetByCode(code); if (byCode.result === true) { if (byCode.entity.course_levels && byCode.entity.course_levels.length) { if (byCode.entity.course_levels.findIndex(l => l.id === levelId) === -1) { return {result: false, entity: "errors.discountNotForCourse"}; } if (byCode.entity.maxUseCount) { const list = await strapi.services["user-course"].find( { course_levels: [levelId], discount: byCode.entity.id, isConfirm: true }); if (list && list.length >= byCode.entity.maxUseCount) { return {result: false, entity: "errors.discountMaxUseCount"}; } } } } return byCode; } };