Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 5a93dcc

Browse files
authored
[BACK-755] Add an extra partnership query (#327)
The frontend is having a hard time refreshing the partnership component if the CollectionPartnerAssociation entity is queried by the value of its 'externalId' field. To fix this, I've added a new query getCollectionPartnerAssociationForCollection - this one requests a collection external id rather the association's own external id. I have left the original getCollectionPartnerAssociation intact even though it's currently not used anywhere on the frontend.
1 parent e82ac3b commit 5a93dcc

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

schema-admin.graphql

+5
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ type Query {
257257
Retrieves a CollectionPartnerAssociation by externalId
258258
"""
259259
getCollectionPartnerAssociation(externalId: String!): CollectionPartnerAssociation
260+
"""
261+
Retrieves a CollectionPartnerAssociation by the externalId of the collection
262+
it is related to.
263+
"""
264+
getCollectionPartnerAssociationForCollection(externalId: String!): CollectionPartnerAssociation
260265
}
261266

262267
type Mutation {

src/admin/resolvers/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getLanguages,
1111
getIABCategories,
1212
searchCollections,
13+
getCollectionPartnerAssociationForCollection,
1314
} from './queries';
1415
import {
1516
collectionImageUpload,
@@ -64,6 +65,7 @@ export const resolvers = {
6465
getCollectionPartner,
6566
getCollectionPartners,
6667
getCollectionPartnerAssociation,
68+
getCollectionPartnerAssociationForCollection,
6769
getCollectionStory,
6870
getCurationCategories,
6971
getIABCategories,

src/admin/resolvers/queries.ts

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getAuthors,
1010
getCollection as dbGetCollection,
1111
getCollectionPartnerAssociation as dbGetCollectionPartnerAssociation,
12+
getCollectionPartnerAssociationForCollection as dbGetCollectionPartnerAssociationForCollection,
1213
getCollectionStory as dbGetCollectionStory,
1314
getCurationCategories as dbGetCurationCategories,
1415
searchCollections as dbSearchCollections,
@@ -202,3 +203,16 @@ export async function getCollectionPartnerAssociation(
202203
): Promise<CollectionPartnerAssociation> {
203204
return dbGetCollectionPartnerAssociation(db, externalId);
204205
}
206+
207+
/**
208+
* @param parent
209+
* @param externalId
210+
* @param db
211+
*/
212+
export async function getCollectionPartnerAssociationForCollection(
213+
parent,
214+
{ externalId },
215+
{ db }
216+
): Promise<CollectionPartnerAssociation> {
217+
return dbGetCollectionPartnerAssociationForCollection(db, externalId);
218+
}

src/database/queries.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ export {
2121
getPartner,
2222
getPartners,
2323
} from './queries/CollectionPartner';
24-
export { getCollectionPartnerAssociation } from './queries/CollectionPartnerAssociation';
24+
export {
25+
getCollectionPartnerAssociation,
26+
getCollectionPartnerAssociationForCollection,
27+
} from './queries/CollectionPartnerAssociation';

src/database/queries/CollectionPartnerAssociation.integration.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { CollectionPartnershipType, PrismaClient } from '@prisma/client';
2-
import { getCollectionPartnerAssociation } from './CollectionPartnerAssociation';
2+
import {
3+
getCollectionPartnerAssociation,
4+
getCollectionPartnerAssociationForCollection,
5+
} from './CollectionPartnerAssociation';
36
import {
47
clear as clearDb,
8+
createAuthorHelper,
9+
createCollectionHelper,
510
createCollectionPartnerAssociationHelper,
611
} from '../../test/helpers';
712

@@ -43,4 +48,42 @@ describe('queries: CollectionPartnerAssociation', () => {
4348
expect(found).toBeNull();
4449
});
4550
});
51+
52+
describe('getCollectionPartnerAssociationForCollection', () => {
53+
it('should get an association by the externalId of the collection it references', async () => {
54+
const author = await createAuthorHelper(db, 'Any Name');
55+
const collection = await createCollectionHelper(db, {
56+
title: 'A test collection',
57+
author,
58+
});
59+
60+
const association = await createCollectionPartnerAssociationHelper(db, {
61+
type: CollectionPartnershipType.PARTNERED,
62+
collection,
63+
});
64+
65+
const found = await getCollectionPartnerAssociationForCollection(
66+
db,
67+
collection.externalId
68+
);
69+
70+
expect(found).not.toBeNull();
71+
expect(association.externalId).toEqual(found.externalId);
72+
});
73+
74+
it('should fail on an invalid collection externalId', async () => {
75+
const author = await createAuthorHelper(db, 'Any Name');
76+
const collection = await createCollectionHelper(db, {
77+
title: 'A test collection without a partnership',
78+
author,
79+
});
80+
81+
const found = await getCollectionPartnerAssociationForCollection(
82+
db,
83+
collection.externalId
84+
);
85+
86+
expect(found).toBeNull();
87+
});
88+
});
4689
});

src/database/queries/CollectionPartnerAssociation.ts

+28
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,31 @@ export async function getCollectionPartnerAssociation(
2323

2424
return association;
2525
}
26+
27+
/**
28+
* @param db
29+
* @param externalId
30+
*/
31+
export async function getCollectionPartnerAssociationForCollection(
32+
db: PrismaClient,
33+
externalId: string
34+
): Promise<CollectionPartnerAssociation> {
35+
const association = db.collectionPartnership.findFirst({
36+
where: {
37+
collection: {
38+
externalId,
39+
},
40+
},
41+
include: {
42+
partner: true,
43+
},
44+
});
45+
46+
if (!association) {
47+
throw new Error(
48+
`Association for collection with external ID: ${externalId} does not exist.`
49+
);
50+
}
51+
52+
return association;
53+
}

0 commit comments

Comments
 (0)