diff --git a/src/admin/controllers/announce.controller.ts b/src/admin/controllers/announce.controller.ts new file mode 100644 index 0000000..8a99c70 --- /dev/null +++ b/src/admin/controllers/announce.controller.ts @@ -0,0 +1,59 @@ +import BaseController from '../../common/base.controller' +import { permission, router } from '../../decorators/router' +import { ZError } from '../../common/ZError' +import { ObjectId } from 'bson' +import { SysAnnounce } from '../../models/content/SysAnnounce' + +export default class AnnounceController extends BaseController { + @permission('shopannounce:read') + @router('post /api/announces') + async list(req, res) { + return SysAnnounce.pageQuery(req.params, true) + } + + @permission('shopannounce:read') + @router('get /api/announce/:id') + async detail(req, res) { + let { id } = req.params + const record = await SysAnnounce.findById(id) + if (!record) { + throw new ZError(11, 'record not found') + } + return record.toJson() + } + + @permission('shopannounce:edit') + @router('post /api/announce/save') + async save(req: any) { + const data = req.params + if (!data._id) { + data.createdBy = req.user.id + data._id = new ObjectId() + } + let record = await SysAnnounce.insertOrUpdate({ _id: data._id }, data) + return record.toJson() + } + + @permission('shopannounce:delete') + @router('post /api/announce/delete') + async delete(req: any) { + let { ids } = req.params + if (!ids) { + throw new ZError(11, 'params mismatch') + } + return SysAnnounce.deleteVirtual(ids) + } + + @permission(['shopannounce:publish']) + @router('post /api/announce/publish') + async publish(req: any) { + let { id, active } = req.params + let record = await SysAnnounce.findById(id) + if (!record) { + throw new ZError(11, 'record not found') + } + record.publish = active + await record.save() + return record.toJson() + } +} diff --git a/src/api/controllers/puzzle.controller.ts b/src/api/controllers/puzzle.controller.ts index f06c52c..10fc409 100644 --- a/src/api/controllers/puzzle.controller.ts +++ b/src/api/controllers/puzzle.controller.ts @@ -377,7 +377,7 @@ class PuzzleController extends BaseController { if (!history) { throw new ZError(13, 'not found match info') } - if (!history.hasExpired()) { + if (history.hasExpired()) { throw new ZError(18, 'match has expired') } if (history.type !== 0) { diff --git a/src/models/content/SysAnnounce.ts b/src/models/content/SysAnnounce.ts new file mode 100644 index 0000000..126d2d1 --- /dev/null +++ b/src/models/content/SysAnnounce.ts @@ -0,0 +1,100 @@ +import { dbconn } from '../../decorators/dbconn' +import { getModelForClass, modelOptions, prop, ReturnModelType } from '@typegoose/typegoose' +import { Severity } from '@typegoose/typegoose/lib/internal/constants' +import { BaseModule } from '../Base' +import { noJson } from '../../decorators/nojson' +import { SysMail } from './SysMail' + +@dbconn() +@modelOptions({ + schemaOptions: { collection: 'sys_announce', timestamps: true }, + options: { allowMixed: Severity.ALLOW }, +}) +export class SysAnnounceClass extends BaseModule { + @prop() + public shop: string + + @prop() + public title: string + + @prop() + public content: string + + /** + * 类型 + * @type {number} 0: 普通邮件 1: 店铺群发 2: 全体 + */ + @prop() + public type: number + + @prop({ default: false }) + public publish: boolean + + @prop({ type: () => [String] }) + public accounts: string[] + + @prop({ type: () => [String] }) + public shops: string[] + + /** + * 是否删除 + * @type {boolean} + */ + @noJson() + @prop({ default: false }) + public deleted: boolean + @noJson() + @prop() + public deleteTime: Date + + /** + * 创建人 + * @type {string} + */ + @prop() + public createdBy: string + + @prop() + public sendTime: number + + @prop() + public endTime: number + + public static async findRecords( + this: ReturnModelType, + accountId: string, + shop: string, + excludes: string[], + ) { + let now = Date.now() + let mails0 = await this.find({ + accounts: accountId, + type: 0, + deleted: false, + publish: true, + sendTime: { $lte: now }, + endTime: { $gte: now }, + _id: { $nin: excludes }, + }) + let mails1 = await this.find({ + shops: shop, + type: 1, + deleted: false, + publish: true, + sendTime: { $lte: now }, + endTime: { $gte: now }, + _id: { $nin: excludes }, + }) + let mails2 = await this.find({ + type: 2, + deleted: false, + publish: true, + sendTime: { $lte: now }, + endTime: { $gte: now }, + _id: { $nin: excludes }, + }) + return mails0.concat(mails1).concat(mails2) + } +} + +export const SysAnnounce = getModelForClass(SysAnnounceClass, { existingConnection: SysAnnounceClass.db })