import DataType from 'sequelize';
import Model from '../sequelize';
import * as util from './util';

const Unit = Model.define('unit', {
  id: {
    type: DataType.UUID,
    defaultValue: DataType.UUIDV1,
    primaryKey: true,
  },

  title: {
    type: DataType.STRING(255),
    allowNull: false,
  },

  body: {
    type: DataType.STRING,
  },

  schema: {
    type: DataType.STRING,
  },
});

Unit.prototype.canRead = async function canRead(user) {
  if (util.haveAccess(user)) return true;
  const courses = await this.getCourses();
  for (let i = 0; i < courses.length; i += 1) {
    // eslint-disable-next-line no-await-in-loop
    const role = await user.getRole(courses[i].id);
    if (!role) return false;
  }
  return true;
};

Unit.prototype.canWrite = function canWrite(user) {
  return util.haveRootAccess(user);
};

export default Unit;

@@ -322,30 +322,28 @@
 e: DataType.
-STRING
+TEXT
 ,%0A  %7D,%0A%0A  sc
@@ -369,22 +369,20 @@
 ataType.
-STRING
+TEXT
 ,%0A  %7D,%0A%7D

fix size of body & schema of unit