增加优惠券相关接口
This commit is contained in:
parent
ac81078c19
commit
eaef5970b6
73
src/admin/controllers/coupon.controller.ts
Normal file
73
src/admin/controllers/coupon.controller.ts
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import BaseController from '../../common/base.controller'
|
||||||
|
import { permission, router } from '../../decorators/router'
|
||||||
|
import { ZError } from '../../common/ZError'
|
||||||
|
import { Coupon } from '../../models/shop/Coupon'
|
||||||
|
|
||||||
|
class CouponController extends BaseController{
|
||||||
|
@permission('coupon:read')
|
||||||
|
@router('post /coupons')
|
||||||
|
async list(req, res) {
|
||||||
|
let { start, limit } = req.params
|
||||||
|
start = +start || 0
|
||||||
|
limit = +limit || 10
|
||||||
|
let { opt, sort } = Coupon.parseQueryParam(req.params)
|
||||||
|
let articles = await Coupon.find(opt)
|
||||||
|
.sort(sort)
|
||||||
|
.skip(start)
|
||||||
|
.limit(limit)
|
||||||
|
let count = await Coupon.count(opt)
|
||||||
|
let records = []
|
||||||
|
for (let record of articles) {
|
||||||
|
records.push(record.toJson())
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
total: count,
|
||||||
|
start,
|
||||||
|
limit,
|
||||||
|
records
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@permission('coupon:read')
|
||||||
|
@router('get /coupon/:id')
|
||||||
|
async detail(req, res) {
|
||||||
|
let { id } = req.params
|
||||||
|
const record = await Coupon.findById(id)
|
||||||
|
if (!record) {
|
||||||
|
throw new ZError(11, 'record not found')
|
||||||
|
}
|
||||||
|
return record.toJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
@permission('coupon:read')
|
||||||
|
@router('post /coupon/save')
|
||||||
|
async save(req: any) {
|
||||||
|
let { _id } = req.params
|
||||||
|
let user = req.user
|
||||||
|
let record
|
||||||
|
if (!_id) {
|
||||||
|
record = new Coupon(req.params)
|
||||||
|
record.createdBy = user.id
|
||||||
|
} else {
|
||||||
|
record = await Coupon.findById(_id)
|
||||||
|
record.updateFromReq(req.params)
|
||||||
|
}
|
||||||
|
await record.save()
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
@permission('coupon:read')
|
||||||
|
@router('post /coupon/:id/delete')
|
||||||
|
async delete(req: any) {
|
||||||
|
let { id } = req.params
|
||||||
|
if (!id) {
|
||||||
|
throw new ZError(11, 'params mismatch')
|
||||||
|
}
|
||||||
|
await Coupon.findByIdAndUpdate(id, {
|
||||||
|
$set: {
|
||||||
|
deleted: true,
|
||||||
|
deleteTime: new Date()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ import { permission, role, router } from '../../decorators/router'
|
|||||||
import { Shop } from '../../models/shop/Shop'
|
import { Shop } from '../../models/shop/Shop'
|
||||||
import { ZError } from '../../common/ZError'
|
import { ZError } from '../../common/ZError'
|
||||||
|
|
||||||
export default class ShopController extends BaseController {
|
class ShopController extends BaseController {
|
||||||
|
|
||||||
@role('admin')
|
@role('admin')
|
||||||
@router('post /shops')
|
@router('post /shops')
|
||||||
@ -29,14 +29,6 @@ export default class ShopController extends BaseController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@role('anon')
|
|
||||||
@router('get /shop/test')
|
|
||||||
async testCreate() {
|
|
||||||
let record = (await Shop.findOrCreate({name: '上海莱悦口腔门诊部'})).doc
|
|
||||||
record.showName = '11'
|
|
||||||
await record.save()
|
|
||||||
return record
|
|
||||||
}
|
|
||||||
|
|
||||||
@permission('shop:read')
|
@permission('shop:read')
|
||||||
@router('get /shop/:id')
|
@router('get /shop/:id')
|
||||||
|
@ -1,12 +1,100 @@
|
|||||||
import { getModelForClass, modelOptions } from '@typegoose/typegoose'
|
import {
|
||||||
|
getModelForClass,
|
||||||
|
index,
|
||||||
|
modelOptions,
|
||||||
|
prop
|
||||||
|
} from '@typegoose/typegoose'
|
||||||
import { dbconn } from '../../decorators/dbconn'
|
import { dbconn } from '../../decorators/dbconn'
|
||||||
import { BaseModule } from '../Base'
|
import { BaseModule } from '../Base'
|
||||||
|
import { noJson } from '../../decorators/nojson'
|
||||||
|
|
||||||
|
|
||||||
@dbconn()
|
@dbconn()
|
||||||
|
@index({ shop:1 }, { unique: false })
|
||||||
@modelOptions({ schemaOptions: { collection: 'shop_coupon', timestamps: true } })
|
@modelOptions({ schemaOptions: { collection: 'shop_coupon', timestamps: true } })
|
||||||
class CouponClass extends BaseModule {
|
class CouponClass extends BaseModule {
|
||||||
|
@prop({ required: true })
|
||||||
|
public name!: string
|
||||||
|
|
||||||
|
@prop()
|
||||||
|
public content: string
|
||||||
|
|
||||||
|
@prop({required: true})
|
||||||
|
public shop: string
|
||||||
|
/**
|
||||||
|
* 已使用
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
@prop({default: 0})
|
||||||
|
public count: number
|
||||||
|
/**
|
||||||
|
* 总数
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public total: number
|
||||||
|
/**
|
||||||
|
* 每人限领n张
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
@prop({default: 0})
|
||||||
|
public limitOne: number
|
||||||
|
/**
|
||||||
|
* 描述
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public comment: string
|
||||||
|
/**
|
||||||
|
* 有效期开始
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public validBegin: number
|
||||||
|
/**
|
||||||
|
* 有效期结束
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public validEnd: number
|
||||||
|
/**
|
||||||
|
* 是否删除
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
|
@noJson()
|
||||||
|
@prop({default: false})
|
||||||
|
public deleted: boolean
|
||||||
|
@noJson()
|
||||||
|
@prop()
|
||||||
|
public deleteTime: Date
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
public createdBy: string
|
||||||
|
|
||||||
|
public static parseQueryParam(params) {
|
||||||
|
let {key, timeBegin, timeEnd, shop} = params
|
||||||
|
let opt: any = {deleted: false}
|
||||||
|
if (key) {
|
||||||
|
opt.name = {$regex: key, $options: 'i'}
|
||||||
|
}
|
||||||
|
if (shop) {
|
||||||
|
opt.shop = shop
|
||||||
|
}
|
||||||
|
if (timeBegin && !timeEnd) {
|
||||||
|
opt.createdAt = {$gte: timeBegin};
|
||||||
|
} else if (timeBegin && timeEnd) {
|
||||||
|
opt['$and'] = [{createdAt: {$gte: timeBegin}}, {createdAt: {$lte: timeEnd}}];
|
||||||
|
} else if (!timeBegin && timeEnd) {
|
||||||
|
opt.createdAt = {$lte: timeEnd};
|
||||||
|
}
|
||||||
|
|
||||||
|
let sort = {_id: -1}
|
||||||
|
return { opt, sort }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Coupon = getModelForClass(CouponClass, { existingConnection: CouponClass.db })
|
export const Coupon = getModelForClass(CouponClass, { existingConnection: CouponClass.db })
|
||||||
|
Loading…
x
Reference in New Issue
Block a user