From 8289f96dd514b7091cd611bd07c8053ac02a84c0 Mon Sep 17 00:00:00 2001 From: Junyi Date: Thu, 7 Jan 2021 14:49:39 +0800 Subject: [PATCH] fix: updatedBy field in bulkUpdate hook (#54) * fix: updatedBy field in bulkUpdate hook * fix: test cases * add db.sync() after each test case Co-authored-by: chenos --- .../plugin-users/src/__tests__/fields.test.ts | 36 ++++++++++++++++++- packages/plugin-users/src/__tests__/index.ts | 7 ++-- packages/plugin-users/src/fields/UpdatedBy.ts | 14 +++++--- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/packages/plugin-users/src/__tests__/fields.test.ts b/packages/plugin-users/src/__tests__/fields.test.ts index c6a2a323d..0e1496a4e 100644 --- a/packages/plugin-users/src/__tests__/fields.test.ts +++ b/packages/plugin-users/src/__tests__/fields.test.ts @@ -9,10 +9,10 @@ describe('user fields', () => { app = await getApp(); agent = getAgent(app); db = app.database; - await db.sync({ force: true }); }); afterEach(async () => { + await db.sync(); await db.close(); }); @@ -283,5 +283,39 @@ describe('user fields', () => { const post2 = await Post.findByPk(post.id); expect(post2.updated_by_id).toBe(user1.id); }); + + it('bulkUpdate', async () => { + const Collection = db.getModel('collections'); + const postCollection = await Collection.create({ name: 'posts', createdBy: false, updatedBy: true }); + await postCollection.updateAssociations({ + fields: [ + { + type: 'string', + name: 'title' + } + ] + }); + const User = db.getModel('users'); + const user1 = await User.create(); + const user2 = await User.create(); + const Post = db.getModel('posts'); + let context = { state: { currentUser: user2 } }; + + await Post.bulkCreate([ + { title: 'title1' }, + { title: 'title2' }, + ]); + + await Post.update({ title: 'title3' }, { + where: { title: 'title1' }, + context + }); + + const posts = await Post.findAll({ order: [['id', 'ASC']] }); + expect(posts.map(({ title, updated_by_id }) => ({ title, updated_by_id }))).toEqual([ + { title: 'title3', updated_by_id: 2 }, + { title: 'title2', updated_by_id: null }, + ]); + }); }); }); diff --git a/packages/plugin-users/src/__tests__/index.ts b/packages/plugin-users/src/__tests__/index.ts index e5514d358..032bc19af 100644 --- a/packages/plugin-users/src/__tests__/index.ts +++ b/packages/plugin-users/src/__tests__/index.ts @@ -5,7 +5,7 @@ import bodyParser from 'koa-bodyparser'; import { Dialect } from 'sequelize'; import Database from '@nocobase/database'; import { actions, middlewares } from '@nocobase/actions'; -import { Application, middleware } from '@nocobase/server'; +import { Application, middleware } from '../../../server/src'; import plugin from '../server'; function getTestKey() { @@ -61,16 +61,13 @@ export async function getApp() { app.registerPlugin('collections', [path.resolve(__dirname, '../../../plugin-collections')]); app.registerPlugin('file-manager', [plugin]); await app.loadPlugins(); - await app.database.sync({ - force: true, - }); + await app.database.sync(); app.use(async (ctx, next) => { ctx.db = app.database; await next(); }); app.use(bodyParser()); app.use(middleware({ - prefix: '/api', resourcer: app.resourcer, database: app.database, })); diff --git a/packages/plugin-users/src/fields/UpdatedBy.ts b/packages/plugin-users/src/fields/UpdatedBy.ts index 5e2ccef25..3afe0f353 100644 --- a/packages/plugin-users/src/fields/UpdatedBy.ts +++ b/packages/plugin-users/src/fields/UpdatedBy.ts @@ -13,10 +13,16 @@ export default class UpdatedBy extends BELONGSTO { }); } - static beforeBulkUpdateHook(this: UpdatedBy, models, { context }) { - models.forEach(model => { - setUserValue.call(this, model, { context }); - }); + static beforeBulkUpdateHook(this: UpdatedBy, { attributes, fields, context }) { + if (!context) { + return; + } + const { currentUser } = context.state; + if (!currentUser) { + return; + } + fields.push(this.options.foreignKey); + attributes[this.options.foreignKey] = currentUser.get(this.options.targetKey); } constructor({ type, ...options }: UpdatedByOptions, context: FieldContext) {