From 43bc29b25cdd061f81a4da16c979e5afbb7573d7 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 13 Jan 2022 11:13:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dopenapi=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=EF=BC=8C=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/data/cron.ts | 5 ++++- back/services/cron.ts | 6 ++++++ back/services/env.ts | 9 ++++++--- back/services/open.ts | 9 +++++---- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/back/data/cron.ts b/back/data/cron.ts index 7dc703f6..56f0ec81 100644 --- a/back/data/cron.ts +++ b/back/data/cron.ts @@ -50,7 +50,10 @@ export enum CrontabStatus { interface CronInstance extends Model, Crontab {} export const CrontabModel = sequelize.define('Crontab', { name: DataTypes.STRING, - command: DataTypes.STRING, + command: { + unique: 'command', + type: DataTypes.STRING, + }, schedule: DataTypes.STRING, timestamp: DataTypes.STRING, saved: DataTypes.BOOLEAN, diff --git a/back/services/cron.ts b/back/services/cron.ts index 8b817505..ec671507 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -31,6 +31,12 @@ export default class CronService { } public async insert(payload: Crontab): Promise { + const cron = await CrontabModel.findOne({ + where: { command: payload.command }, + }); + if (cron) { + return cron; + } return await CrontabModel.create(payload, { returning: true }); } diff --git a/back/services/env.ts b/back/services/env.ts index 61331ebd..a82db1a1 100644 --- a/back/services/env.ts +++ b/back/services/env.ts @@ -108,15 +108,18 @@ export default class EnvService { }; } try { - const result = await this.find(condition); + const result = await this.find(condition, [['position', 'DESC']]); return result as any; } catch (error) { throw error; } } - private async find(query: any, sort?: any): Promise { - const docs = await EnvModel.findAll({ where: { ...query } }); + private async find(query: any, sort: any = []): Promise { + const docs = await EnvModel.findAll({ + where: { ...query }, + order: [...sort], + }); return docs; } diff --git a/back/services/open.ts b/back/services/open.ts index ab044ec0..924b277a 100644 --- a/back/services/open.ts +++ b/back/services/open.ts @@ -12,14 +12,15 @@ export default class OpenService { constructor(@Inject('logger') private logger: winston.Logger) {} public async findTokenByValue(token: string): Promise { - const doc = await this.getDb({ - where: sequelize.fn( + const doc = await this.getDb( + sequelize.fn( 'JSON_CONTAINS', sequelize.col('tokens'), JSON.stringify({ value: token }), ), - }); + ); return doc; + AppModel.upsert; } public async create(payload: App): Promise { @@ -50,7 +51,7 @@ export default class OpenService { } public async getDb(query: any): Promise { - const doc: any = await AppModel.findOne({ where: { ...query } }); + const doc: any = await AppModel.findOne({ where: query }); return doc && (doc.get({ plain: true }) as App); }