Skip to content

Commit

Permalink
Merge pull request #261 from desci-labs/contributor-api-adj
Browse files Browse the repository at this point in the history
Contributor API adjustments
  • Loading branch information
kadamidev authored Mar 25, 2024
2 parents 88221ef + 6804457 commit 9766814
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
10 changes: 7 additions & 3 deletions desci-server/src/controllers/users/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Request, Response } from 'express';

import { prisma } from '../../client.js';
import { logger as parentLogger } from '../../logger.js';
import { orcidRegex } from '../../utils.js';
import { formatOrcidString, orcidRegex } from '../../utils.js';

export type SearchProfilesRequest = Request<never, never, never, { name?: string; orcid?: string }> & {
user: User; // added by auth middleware
Expand All @@ -20,8 +20,10 @@ export type SearchProfilesResBody =
export type UserProfile = { name: string; id: number; orcid?: string };

export const searchProfiles = async (req: SearchProfilesRequest, res: Response<SearchProfilesResBody>) => {
// debugger;
const user = req.user;
const { name, orcid } = req.query;
const { name } = req.query;
let { orcid } = req.query;
const logger = parentLogger.child({
module: 'Users::searchProfiles',
body: req.body,
Expand All @@ -36,7 +38,9 @@ export const searchProfiles = async (req: SearchProfilesRequest, res: Response<S
.status(400)
.json({ error: 'Invalid orcid id, orcid must follow either 123456780000 or 1234-4567-8000-0000 format.' });

if (name.toString().length < 2 && !orcid)
if (orcid) orcid = formatOrcidString(orcid); // Ensure hyphenated

if (name?.toString().length < 2 && !orcid)
return res.status(400).json({ error: 'Name query must be at least 2 characters' });

try {
Expand Down
6 changes: 5 additions & 1 deletion desci-server/src/services/Contributors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { format } from 'path';

import { Node, NodeContribution, User } from '@prisma/client';
import ShortUniqueId from 'short-unique-id';

import { prisma } from '../client.js';
import { logger as parentLogger } from '../logger.js';
import { getIndexedResearchObjects } from '../theGraph.js';
import { hexToCid } from '../utils.js';
import { formatOrcidString, hexToCid } from '../utils.js';

type ContributorId = string;

Expand Down Expand Up @@ -52,6 +54,7 @@ class ContributorService {
orcid,
userId,
}: AddNodeContributionParams): Promise<NodeContribution> {
if (orcid) orcid = formatOrcidString(orcid); // Ensure hyphenated
// Check if contributor is already registered
let registeredContributor;
if (email) registeredContributor = await prisma.user.findUnique({ where: { email } });
Expand Down Expand Up @@ -86,6 +89,7 @@ class ContributorService {
orcid,
userId,
}: AddNodeContributionParams): Promise<NodeContribution> {
if (orcid) orcid = formatOrcidString(orcid); // Ensure hyphenated
// Check if contribution is already verified
let registeredContributor;
if (email) registeredContributor = await prisma.user.findUnique({ where: { email } });
Expand Down
13 changes: 13 additions & 0 deletions desci-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,16 @@ export function omitKeys(obj: Record<string, any>, filterList: string[]): Record
* or 0000000000000000
*/
export const orcidRegex: RegExp = /^\d{4}-?\d{4}-?\d{4}-?\d{3}[\dX]$/;

/**
* Converts a non hyphenated orcid ID to hyphenated format.
*/
export function formatOrcidString(orcidId: string): string {
// Remove any existing hyphens from the input string
const orcidWithoutHyphens = orcidId.replace(/-/g, '');

// Insert hyphens at the appropriate positions
const formattedOrcid = orcidWithoutHyphens.replace(/(\d{4})(\d{4})(\d{4})(\d{3}[\dX])/, '$1-$2-$3-$4');

return formattedOrcid;
}

0 comments on commit 9766814

Please sign in to comment.