import { Context, IMiddleware, Middleware, NextFunction } from '@midwayjs/core';
import { get } from 'lodash';

@Middleware()
export class NeedAdminMiddleware implements IMiddleware<Context, NextFunction> {
  resolve() {
    return async (ctx: Context, next: NextFunction) => {
      const user = get(ctx, 'user');
      if (!user) return;
      const role = get(user, 'role', []);
      const has_admin = role.find(f => f === 'Admin');
      if (!has_admin) return;
      await next();
    };
  }

  static getName(): string {
    return 'needAdmin';
  }
}
