2023-09-26 15:53:00 +00:00
|
|
|
import { column, manyToMany, ManyToMany, SnakeCaseNamingStrategy, beforeUpdate, beforeCreate } from '@ioc:Adonis/Lucid/Orm';
|
2023-03-03 15:54:28 +00:00
|
|
|
import { DateTime } from 'luxon';
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import Role from 'App/Models/Role';
|
2023-09-26 15:53:00 +00:00
|
|
|
import BaseModel from './BaseModel';
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
export default class Permission extends BaseModel {
|
2023-06-22 15:20:04 +00:00
|
|
|
public static namingStrategy = new SnakeCaseNamingStrategy();
|
|
|
|
public static primaryKey = 'id';
|
|
|
|
public static table = 'permissions';
|
|
|
|
public static selfAssignPrimaryKey = false;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column({
|
|
|
|
isPrimary: true,
|
|
|
|
})
|
|
|
|
public id: number;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column({})
|
|
|
|
public role_id: number;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column({})
|
|
|
|
public display_name: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column({})
|
|
|
|
public name: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column({})
|
|
|
|
public description: string;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column.dateTime({
|
|
|
|
serialize: (value: Date | null) => {
|
|
|
|
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
|
|
|
|
},
|
|
|
|
autoCreate: true,
|
|
|
|
})
|
|
|
|
public created_at: DateTime;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@column.dateTime({
|
|
|
|
serialize: (value: Date | null) => {
|
|
|
|
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
|
|
|
|
},
|
|
|
|
autoCreate: true,
|
|
|
|
autoUpdate: true,
|
|
|
|
})
|
|
|
|
public updated_at: DateTime;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@beforeCreate()
|
|
|
|
@beforeUpdate()
|
|
|
|
public static async resetDate(role) {
|
|
|
|
role.created_at = this.formatDateTime(role.created_at);
|
|
|
|
role.updated_at = this.formatDateTime(role.updated_at);
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// public static boot() {
|
|
|
|
// super.boot()
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// this.before('create', async (_modelInstance) => {
|
|
|
|
// _modelInstance.created_at = this.formatDateTime(_modelInstance.created_at)
|
|
|
|
// _modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at)
|
|
|
|
// })
|
|
|
|
// this.before('update', async (_modelInstance) => {
|
|
|
|
// _modelInstance.created_at = this.formatDateTime(_modelInstance.created_at)
|
|
|
|
// _modelInstance.updated_at = this.formatDateTime(_modelInstance.updated_at)
|
|
|
|
// })
|
|
|
|
// }
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
private static formatDateTime(datetime) {
|
|
|
|
let value = new Date(datetime);
|
|
|
|
return datetime
|
|
|
|
? value.getFullYear() +
|
|
|
|
'-' +
|
|
|
|
(value.getMonth() + 1) +
|
|
|
|
'-' +
|
|
|
|
value.getDate() +
|
|
|
|
' ' +
|
|
|
|
value.getHours() +
|
|
|
|
':' +
|
|
|
|
value.getMinutes() +
|
|
|
|
':' +
|
|
|
|
value.getSeconds()
|
|
|
|
: datetime;
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// @belongsTo(() => Role)
|
|
|
|
// public role: BelongsTo<typeof Role>;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
@manyToMany(() => Role, {
|
|
|
|
pivotForeignKey: 'permission_id',
|
|
|
|
pivotRelatedForeignKey: 'role_id',
|
|
|
|
pivotTable: 'role_has_permissions',
|
|
|
|
})
|
|
|
|
public roles: ManyToMany<typeof Role>;
|
2023-03-03 15:54:28 +00:00
|
|
|
}
|