Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Orcid Work record invalid Dpid url #514

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions desci-server/src/services/orcid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class OrcidApiService {
throw new Error('User does not have an orcid auth token');
}

// todo: refresh token if necessary
try {
const url = `https://${ORCID_DOMAIN}/oauth/token`;

Expand Down Expand Up @@ -125,6 +124,9 @@ class OrcidApiService {
* @returns
*/
async removeClaimRecord({ claimId, nodeUuid, orcid }: { claimId: number; nodeUuid: string; orcid: string }) {
const user = await prisma.user.findUnique({ where: { orcid } });
const authToken = await this.getAccessToken(user.id);

const putCode = await prisma.orcidPutCodes.findFirst({
where: {
claimId,
Expand All @@ -133,18 +135,13 @@ class OrcidApiService {
},
});

if (!putCode) return;

const user = await prisma.user.findUnique({ where: { orcid } });
const authToken = await this.getAccessToken(user.id);
logger.info({ userId: user.id, authToken: !!authToken, nodeUuid }, '[ORCID::DELETE]:: START');
// remove claim record on Orcid profile
if (putCode) {
logger.info({ userId: user.id, authToken: !!authToken, nodeUuid }, '[ORCID::DELETE]:: START');

await this.removeWorkRecord({ orcid, putCode, authToken });
await this.removeWorkRecord({ orcid, putCode, authToken });
}

const { researchObjects } = await getIndexedResearchObjects([nodeUuid]);
const researchObject = researchObjects[0] as IndexedResearchObject;
const manifestCid = hexToCid(researchObject.recentCid);
const latestManifest = await getManifestByCid(manifestCid);
let claims = await attestationService.getProtectedNodeClaims(nodeUuid);
claims = claims.filter((claim) => claim.verifications > 0);
logger.info({ claims: claims.length }, '[ORCID::DELETE]:: CHECK CLAIMS');
Expand Down Expand Up @@ -185,7 +182,7 @@ class OrcidApiService {
code,
orcid,
},
'ORCID API DELETE RECORD',
'ORCID API DELETE WORK RECORD',
);
const response = await fetch(url, {
method: 'DELETE',
Expand Down Expand Up @@ -253,8 +250,14 @@ class OrcidApiService {
let claims = await attestationService.getProtectedNodeClaims(nodeUuid);
claims = claims.filter((claim) => claim.verifications > 0);

// TODO: if claims is empty remove orcid record
if (claims.length === 0) return;
if (claims.length === 0) {
const orcidPutCode = await prisma.orcidPutCodes.findUnique({
where: { orcid_uuid_reference: { orcid, uuid: nodeUuid, reference: PutcodeReference.PREPRINT } },
});

await orcidApiService.removeWorkRecord({ authToken, putCode: orcidPutCode, orcid: user.orcid });
return;
}

const latestVersion = researchObject.versions[researchObject.versions.length - 1];
const publicationDate = new Date(parseInt(latestVersion.time) * 1000).toLocaleDateString().replaceAll('/', '-');
Expand All @@ -270,8 +273,9 @@ class OrcidApiService {
manifest: latestManifest,
});
const claimRecordPromises = claims.map((claim) => {
const claimedVersionNumber = claims[claims.length - 1].nodeVersion;
const claimedVersion = researchObject.versions[claimedVersionNumber];
const versionIndex = claims[claims.length - 1].nodeVersion;
const claimedVersionNumber = versionIndex + 1;
const claimedVersion = researchObject.versions[versionIndex];
const publicationDate = new Date(parseInt(claimedVersion.time) * 1000)
.toLocaleDateString()
.replaceAll('/', '-');
Expand Down
22 changes: 16 additions & 6 deletions desci-server/src/theGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const getIndexedResearchObjects = async (
For stream resolution, build a map to allow for also returning the UUID
to match the format returned by the graph lookup
*/
const streamLookupMap: Record<string, string> = {};
let streamLookupMap: Record<string, string> = {};
/** For legacy nodes, the graph lookup only needs the UUID */
const legacyUuids = [];

Expand All @@ -100,6 +100,16 @@ export const getIndexedResearchObjects = async (
}
}

/**
* fallback to _getIndexedResearchObjects() when resolving locally
* because calls to getHistoryFromStreams() never returns due to
* RESOLVER_URL not configured for local dpid resolution
*/
if (process.env.NODE_ENV === 'dev') {
legacyUuids.push(...paddedUuids);
streamLookupMap = {};
}

let streamHistory = [];
if (Object.keys(streamLookupMap).length > 0) {
logger.info({ streamLookupMap }, 'Querying resolver for history');
Expand All @@ -109,7 +119,7 @@ export const getIndexedResearchObjects = async (

let legacyHistory = [];
if (legacyUuids.length > 0) {
logger.info({ legacyUuids }, 'Falling back to subgraph query for history');
logger.info({ legacyUuids, _urlSafeBase64s }, 'Falling back to subgraph query for history');
legacyHistory = (await _getIndexedResearchObjects(legacyUuids)).researchObjects;
logger.info({ legacyHistory }, 'Subgraph history for nodes found');
}
Expand Down Expand Up @@ -201,7 +211,7 @@ export const _getIndexedResearchObjects = async (
export const getTimeForTxOrCommits = async (txOrCommits: string[]): Promise<Record<string, string>> => {
const isTx = (id: string) => id.startsWith('0x');
const txIds = txOrCommits.filter(isTx);
const commitIdStrs = txOrCommits.filter(id => !isTx(id));
const commitIdStrs = txOrCommits.filter((id) => !isTx(id));

const commitTimeMap = await getCommitTimestamps(commitIdStrs);
const txTimeMap = await getTxTimestamps(txIds);
Expand All @@ -221,15 +231,15 @@ const getTxTimestamps = async (txIds: string[]): Promise<Record<string, string>>
try {
const graphTxTimestamps = await getTxTimeFromGraph(txIds);
const timeMap = graphTxTimestamps.reduce(
(acc, { id, time }) => ({ ...acc, [id]: time}),
(acc, { id, time }) => ({ ...acc, [id]: time }),
{} as Record<string, string>,
);
return timeMap;
} catch (err) {
logger.error({ txIds, err }, 'failed to get tx timestamps from graph, returning empty map');
return {};
};
}
}
};

type TransactionsWithTimestamp = {
researchObjectVersions: { id: string; time: string }[];
Expand Down