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 <chenlinxh@gmail.com>
This commit is contained in:
parent
80acf5f199
commit
8289f96dd5
@ -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 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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,
|
||||
}));
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user