tethys.backend/app/Models/Dataset.ts

274 lines
7.9 KiB
TypeScript
Raw Normal View History

2023-03-03 15:54:28 +00:00
import {
column,
SnakeCaseNamingStrategy,
manyToMany,
belongsTo,
hasMany,
computed,
2024-03-14 19:25:27 +00:00
hasOne
} from '@adonisjs/lucid/orm';
2023-03-03 15:54:28 +00:00
import { DateTime } from 'luxon';
import dayjs from 'dayjs';
2024-03-14 19:25:27 +00:00
import Person from './Person.js';
import User from './User.js';
import Title from './Title.js';
import Description from './Description.js';
import License from './License.js';
import Subject from './Subject.js';
import File from './File.js';
import Coverage from './Coverage.js';
import DatasetReference from './DatasetReference.js';
import Collection from './Collection.js';
import DatasetIdentifier from './DatasetIdentifier.js';
import Project from './Project.js';
import DocumentXmlCache from './DocumentXmlCache.js';
import DatasetExtension from '#app/Models/Traits/DatasetExtension';
import type { ManyToMany } from "@adonisjs/lucid/types/relations";
import type { BelongsTo } from "@adonisjs/lucid/types/relations";
import type { HasMany } from "@adonisjs/lucid/types/relations";
import type { HasOne } from "@adonisjs/lucid/types/relations";
2023-03-03 15:54:28 +00:00
export default class Dataset extends DatasetExtension {
public static namingStrategy = new SnakeCaseNamingStrategy();
public static primaryKey = 'id';
public static table = 'documents';
public static selfAssignPrimaryKey = false;
2023-03-03 15:54:28 +00:00
@column({ isPrimary: true })
public id: number;
2023-03-03 15:54:28 +00:00
@column({})
public server_state: string;
2023-03-03 15:54:28 +00:00
@column({})
public publisher_name: string;
@column({ columnName: 'creating_corporation' })
public creating_corporation: string;
2023-03-03 15:54:28 +00:00
@column.dateTime({ columnName: 'embargo_date' })
public embargo_date: DateTime;
2023-03-03 15:54:28 +00:00
@column({})
public type: string;
2023-03-03 15:54:28 +00:00
@column({})
public language: string;
@column({columnName: 'publish_id'})
public publish_id: number | null = null;
@column({})
public project_id: number | null = null;
@column({})
public account_id: number | null = null;
2023-03-03 15:54:28 +00:00
@column({})
public editor_id: number | null = null;
@column({})
public reviewer_id: number | null = null;
@column({})
public reject_editor_note: string | null;
@column({})
public preferred_reviewer: string | null;
@column({})
public preferred_reviewer_email: string | null;
@column({})
public reject_reviewer_note: string | null;
@column.dateTime({ columnName: 'server_date_published' })
public server_date_published: DateTime;
2023-03-03 15:54:28 +00:00
// @column.dateTime({ autoCreate: true, columnName: 'created_at' })
@column.dateTime({
serialize: (value: Date | null) => {
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
},
autoCreate: true,
columnName: 'created_at',
})
public created_at: DateTime;
2023-03-03 15:54:28 +00:00
@column.dateTime({
serialize: (value: Date | null) => {
return value ? dayjs(value).format('MMMM D YYYY HH:mm a') : value;
},
autoCreate: true,
autoUpdate: true,
columnName: 'server_date_modified',
})
public server_date_modified: DateTime;
2023-03-03 15:54:28 +00:00
@manyToMany(() => Person, {
pivotForeignKey: 'document_id',
pivotRelatedForeignKey: 'person_id',
pivotTable: 'link_documents_persons',
pivotColumns: ['role', 'sort_order', 'allow_email_contact'],
})
public persons: ManyToMany<typeof Person>;
/**
* Get the account that the dataset belongs to
*/
@belongsTo(() => User, {
foreignKey: 'account_id',
})
public user: BelongsTo<typeof User>;
@belongsTo(() => Project, {
foreignKey: 'project_id',
})
public project: BelongsTo<typeof Project>;
@hasMany(() => Title, {
foreignKey: 'document_id',
})
public titles: HasMany<typeof Title>;
@hasMany(() => Description, {
foreignKey: 'document_id',
})
public descriptions: HasMany<typeof Description>;
@manyToMany(() => License, {
pivotForeignKey: 'document_id',
pivotRelatedForeignKey: 'licence_id',
pivotTable: 'link_documents_licences',
})
public licenses: ManyToMany<typeof License>;
@manyToMany(() => Subject, {
pivotForeignKey: 'document_id',
pivotRelatedForeignKey: 'subject_id',
pivotTable: 'link_dataset_subjects',
})
public subjects: ManyToMany<typeof Subject>;
2023-03-03 15:54:28 +00:00
@hasMany(() => File, {
foreignKey: 'document_id',
})
public files: HasMany<typeof File>;
@hasOne(() => Coverage, {
foreignKey: 'dataset_id',
})
public coverage: HasOne<typeof Coverage>;
@hasMany(() => DatasetReference, {
foreignKey: 'document_id',
})
public references: HasMany<typeof DatasetReference>;
// Dataset.hasMany(Reference, {
// foreignKey: "related_document_id",
// as: "referenced_by",
// });
@hasMany(() => DatasetReference, {
foreignKey: 'related_document_id',
})
public referenced_by: HasMany<typeof DatasetReference>;
@manyToMany(() => Collection, {
pivotForeignKey: 'document_id',
pivotRelatedForeignKey: 'collection_id',
pivotTable: 'link_documents_collections',
})
public collections: ManyToMany<typeof Collection>;
@hasOne(() => DatasetIdentifier, {
foreignKey: 'dataset_id',
})
public identifier: HasOne<typeof DatasetIdentifier>;
@computed({
serializeAs: 'main_title',
})
public get mainTitle() {
// return `${this.firstName} ${this.lastName}`;
const mainTitle = this.titles?.find((title) => title.type === 'Main');
return mainTitle ? mainTitle.value : null;
}
@computed({
serializeAs: 'main_abstract',
})
public get mainAbstract() {
// return `${this.firstName} ${this.lastName}`;
const mainTitle = this.descriptions?.find((desc) => desc.type === 'Abstract');
return mainTitle ? mainTitle.value : null;
}
@manyToMany(() => Person, {
pivotForeignKey: 'document_id',
pivotRelatedForeignKey: 'person_id',
pivotTable: 'link_documents_persons',
pivotColumns: ['role', 'sort_order', 'allow_email_contact'],
onQuery(query) {
query.wherePivot('role', 'author');
},
})
public authors: ManyToMany<typeof Person>;
@manyToMany(() => Person, {
pivotForeignKey: 'document_id',
pivotRelatedForeignKey: 'person_id',
pivotTable: 'link_documents_persons',
pivotColumns: ['role', 'sort_order', 'allow_email_contact', 'contributor_type'],
onQuery(query) {
query.wherePivot('role', 'contributor');
},
})
public contributors: ManyToMany<typeof Person>;
@hasOne(() => DocumentXmlCache, {
foreignKey: 'document_id',
})
public xmlCache: HasOne<typeof DocumentXmlCache>;
/**
* Get the account that the dataset belongs to
*/
@belongsTo(() => User, {
foreignKey: 'editor_id',
})
public editor: BelongsTo<typeof User>;
@belongsTo(() => User, {
foreignKey: 'reviewer_id',
})
public reviewer: BelongsTo<typeof User>;
static async earliestPublicationDate(): Promise<Dataset | null> {
const serverState = 'published';
const model = await this.query().where('server_state', serverState).orderBy('server_date_published', 'asc').first();
return model || null;
}
static async getMax (column: string) {
let dataset = await this.query().max(column + ' as max_publish_id').firstOrFail();
return dataset.$extras.max_publish_id;
}
@computed({
serializeAs: 'remaining_time',
})
public get remainingTime() {
const dateFuture = this.server_date_modified.plus({ days: 14 });
if (this.server_state === 'approved') {
const now = DateTime.now();
let duration = dateFuture.diff(now, ['days', 'hours', 'months']).toObject();
return duration.days;
} else {
return 0;
}
}
2023-03-03 15:54:28 +00:00
}