Skip to content

Commit

Permalink
mirage updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bp-cos committed Aug 25, 2023
1 parent ae03b73 commit b9ec4dd
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/models/contributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -53,6 +54,9 @@ export default class ContributorModel extends OsfModel.extend(Validations) {
@belongsTo('node', { inverse: 'contributors', polymorphic: true })
node!: AsyncBelongsTo<NodeModel> & NodeModel;

@belongsTo('preprint', { inverse: 'contributors'})
preprint!: AsyncBelongsTo<PreprintModel> & PreprintModel;

@belongsTo('draft-registration', { inverse: 'contributors' })
draftRegistration!: AsyncBelongsTo<DraftRegistrationModel> & DraftRegistrationModel;

Expand Down
6 changes: 3 additions & 3 deletions app/models/preprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> & NodeModel;

@belongsTo('license', { inverse: null })
Expand All @@ -40,8 +40,8 @@ export default class PreprintModel extends OsfModel {
@hasMany('review-action', { inverse: 'target' })
reviewActions!: AsyncHasMany<ReviewActionModel>;

@hasMany('contributor')
contributors!: AsyncHasMany<ContributorModel>;
@hasMany('contributors', { inverse: 'preprint'})
contributors!: AsyncHasMany<ContributorModel> & ContributorModel;

@hasMany('subject', { inverse: null, async: false })
subjects!: SyncHasMany<SubjectModel>;
Expand Down
4 changes: 2 additions & 2 deletions app/preprints/detail/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion app/preprints/detail/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -63,6 +63,7 @@ export default class PreprintsDetail extends Route {

return {
preprint,
contributors,
};

} catch (error) {
Expand Down
10 changes: 10 additions & 0 deletions mirage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
12 changes: 12 additions & 0 deletions mirage/factories/preprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export default Factory.extend<PreprintModel>({
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(),
Expand Down
12 changes: 9 additions & 3 deletions mirage/serializers/contributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ type MirageContributor = Contributor & { attrs: ContributorAttrs };

export default class ContributorSerializer extends ApplicationSerializer<MirageContributor> {
buildNormalLinks(model: ModelInstance<MirageContributor>) {
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,
};
Expand Down
7 changes: 7 additions & 0 deletions mirage/serializers/preprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export default class PreprintSerializer extends ApplicationSerializer<PreprintMo
},
},
},
contributors: {
links: {
related: {
href: `${apiUrl}/v2/preprints/${model.id}/contributors`,
},
},
},
/*
subjects: {
links: {
Expand Down

0 comments on commit b9ec4dd

Please sign in to comment.