From b9ec4dd1be5ac0d79d1a0af627d549b8988fb989 Mon Sep 17 00:00:00 2001 From: Brian Pilati Date: Fri, 25 Aug 2023 12:58:13 -0600 Subject: [PATCH] mirage updates --- app/models/contributor.ts | 4 ++++ app/models/preprint.ts | 6 +++--- app/preprints/detail/controller.ts | 4 ++-- app/preprints/detail/route.ts | 3 ++- mirage/config.ts | 10 ++++++++++ mirage/factories/preprint.ts | 12 ++++++++++++ mirage/serializers/contributor.ts | 12 +++++++++--- mirage/serializers/preprint.ts | 7 +++++++ 8 files changed, 49 insertions(+), 9 deletions(-) diff --git a/app/models/contributor.ts b/app/models/contributor.ts index 4b2007fd81..e311e9dd5c 100644 --- a/app/models/contributor.ts +++ b/app/models/contributor.ts @@ -3,6 +3,7 @@ import { not } from '@ember/object/computed'; import { buildValidations, validator } from 'ember-cp-validations'; import DraftRegistrationModel from './draft-registration'; +import PreprintModel from './preprint'; import NodeModel from './node'; import OsfModel, { Permission } from './osf-model'; import UserModel from './user'; @@ -53,6 +54,9 @@ export default class ContributorModel extends OsfModel.extend(Validations) { @belongsTo('node', { inverse: 'contributors', polymorphic: true }) node!: AsyncBelongsTo & NodeModel; + @belongsTo('preprint', { inverse: 'contributors'}) + preprint!: AsyncBelongsTo & PreprintModel; + @belongsTo('draft-registration', { inverse: 'contributors' }) draftRegistration!: AsyncBelongsTo & DraftRegistrationModel; diff --git a/app/models/preprint.ts b/app/models/preprint.ts index dd312e80a3..d559d5af5d 100644 --- a/app/models/preprint.ts +++ b/app/models/preprint.ts @@ -25,7 +25,7 @@ export default class PreprintModel extends OsfModel { @attr('date') dateLastTransitioned!: Date; @attr('date') preprintDoiCreated!: Date; - @belongsTo('node', { inverse: 'preprints' }) + @belongsTo('node', { inverse: 'preprint' }) node!: AsyncBelongsTo & NodeModel; @belongsTo('license', { inverse: null }) @@ -40,8 +40,8 @@ export default class PreprintModel extends OsfModel { @hasMany('review-action', { inverse: 'target' }) reviewActions!: AsyncHasMany; - @hasMany('contributor') - contributors!: AsyncHasMany; + @hasMany('contributors', { inverse: 'preprint'}) + contributors!: AsyncHasMany & ContributorModel; @hasMany('subject', { inverse: null, async: false }) subjects!: SyncHasMany; diff --git a/app/preprints/detail/controller.ts b/app/preprints/detail/controller.ts index e9e146a7fd..dda1291362 100644 --- a/app/preprints/detail/controller.ts +++ b/app/preprints/detail/controller.ts @@ -114,8 +114,8 @@ export default class PrePrintsDetailController extends Controller { return this.model.subjects.reduce((acc: SubjectModel[], val: SubjectModel) => acc.concat(val), []).uniqBy('id'); } - authors(): ContributorModel[] { - return this.model.preprint.contributors; + get authors(): ContributorModel[] { + return this.model.contributors; } fullLicenseText(): string { diff --git a/app/preprints/detail/route.ts b/app/preprints/detail/route.ts index 22de66e39f..fb7fb3dbe9 100644 --- a/app/preprints/detail/route.ts +++ b/app/preprints/detail/route.ts @@ -49,7 +49,7 @@ export default class PreprintsDetail extends Route { this.theme.set('id', provider.id); - // const contributors = await preprint?.queryHasMany('contributors'); + const contributors = await preprint?.queryHasMany('contributors'); // const license = await preprint?.queryHasMany('license'); @@ -63,6 +63,7 @@ export default class PreprintsDetail extends Route { return { preprint, + contributors, }; } catch (error) { diff --git a/mirage/config.ts b/mirage/config.ts index d13433847b..17d5667ece 100644 --- a/mirage/config.ts +++ b/mirage/config.ts @@ -304,7 +304,17 @@ export default function(this: Server) { path: '/providers/preprints/:parentID/preprints/', relatedModelName: 'preprint', }); + + /** + * Preprint Details + */ + osfResource(this, 'preprint'); + osfNestedResource(this, 'preprint', 'contributors', { + path: '/preprints/:parentID/contributors/', + defaultSortKey: 'index', + relatedModelName: 'contributor', + }); osfResource(this, 'registration-provider', { path: '/providers/registrations' }); diff --git a/mirage/factories/preprint.ts b/mirage/factories/preprint.ts index d89600f91f..550723873b 100644 --- a/mirage/factories/preprint.ts +++ b/mirage/factories/preprint.ts @@ -9,6 +9,18 @@ export default Factory.extend({ id: guid('preprint'), afterCreate(newPreprint, server) { guidAfterCreate(newPreprint, server); + + const contributorUser = server.create('user'); + + const contributor = server.create('contributor', { + preprint: newPreprint, + users: contributorUser, + index: 0, + }); + + newPreprint.update({ + contributors: [contributor], + }); }, title: faker.lorem.sentence(), diff --git a/mirage/serializers/contributor.ts b/mirage/serializers/contributor.ts index 19bc2cf8ae..34d8da5612 100644 --- a/mirage/serializers/contributor.ts +++ b/mirage/serializers/contributor.ts @@ -15,9 +15,15 @@ type MirageContributor = Contributor & { attrs: ContributorAttrs }; export default class ContributorSerializer extends ApplicationSerializer { buildNormalLinks(model: ModelInstance) { - const url = model.node - ? `${apiUrl}/v2/nodes/${model.node.id}/contributors/${model.id}` - : `${apiUrl}/v2/draft_registrations/${model.draftRegistration.id}/contributors/${model.id}`; + let url = ''; + if(model.node) { + url = `${apiUrl}/v2/nodes/${model.node.id}/contributors/${model.id}`; + } else if (model.preprint) { + url = `${apiUrl}/v2/preprints/${model.preprint.id}/contributors/${model.id}`; + } else { + url = `${apiUrl}/v2/draft_registrations/${model.draftRegistration.id}/contributors/${model.id}`; + } + return { self: url, }; diff --git a/mirage/serializers/preprint.ts b/mirage/serializers/preprint.ts index 48b4488894..55916c8bc7 100644 --- a/mirage/serializers/preprint.ts +++ b/mirage/serializers/preprint.ts @@ -22,6 +22,13 @@ export default class PreprintSerializer extends ApplicationSerializer