Skip to content

Commit

Permalink
add schema and implement backfill script
Browse files Browse the repository at this point in the history
  • Loading branch information
shadrach-tayo committed Jan 8, 2025
1 parent 50372ee commit 26cd763
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 6 deletions.
17 changes: 17 additions & 0 deletions desci-server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ model Node {
UserNotifications UserNotifications[]
Annotation Annotation[]
ExternalPublications ExternalPublications[]
CommunityRadarEntry CommunityRadarEntry[]
@@index([ownerId])
@@index([uuid])
Expand Down Expand Up @@ -697,6 +698,7 @@ model DesciCommunity {
NodeAttestation NodeAttestation[]
AttestationTemplate AttestationTemplate[]
CommunityEntryAttestation CommunityEntryAttestation[]
CommunityRadarEntry CommunityRadarEntry[]
}

model CommunityMember {
Expand Down Expand Up @@ -799,10 +801,25 @@ model NodeAttestation {
NodeAttestationVerification NodeAttestationVerification[]
NodeAttestationReaction NodeAttestationReaction[]
OrcidPutCodes OrcidPutCodes[]
CommunityRadarEntry CommunityRadarEntry? @relation(fields: [nodeUuid, desciCommunityId], references: [nodeUuid, desciCommunityId])
// communityRadarEntryId Int?
@@unique([nodeUuid, nodeVersion, attestationId, attestationVersionId])
}

model CommunityRadarEntry {
id Int @id @default(autoincrement())
desciCommunityId Int
community DesciCommunity @relation(fields: [desciCommunityId], references: [id])
nodeUuid String
node Node @relation(fields: [nodeUuid], references: [uuid])
nodeAttestations NodeAttestation[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([nodeUuid, desciCommunityId])
}

// Deferred emails are usually used when a published node is required, e.g. unpublished attestation claims, emails are deferred until node is published.
model DeferredEmails {
id Int @id @default(autoincrement())
Expand Down
4 changes: 3 additions & 1 deletion desci-server/src/controllers/attestations/claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ export const claimAttestation = async (req: RequestWithUser, res: Response, _nex
nodeDpid: string;
claimerId: number;
};

// TODO: verify attestationVersions[0] === latest
const attestationVersions = await attestationService.getAttestationVersions(body.attestationId);
const latest = attestationVersions.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
const attestationVersion = latest[0];
const attestationVersion = attestationVersions[0];
logger.info({ body, latest, attestationVersion }, 'CLAIM');
const uuid = ensureUuidEndsWithDot(body.nodeUuid);

Expand Down
4 changes: 0 additions & 4 deletions desci-server/src/controllers/communities/radar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export const getCommunityRadar = async (req: Request, res: Response, next: NextF
// THIS is necessary because the engagement signal returned from getCommunityRadar
// accounts for only engagements on community selected attestations
const nodes = await asyncMap(communityRadar, async (node) => {
// const engagements = await communityService.getNodeCommunityEngagementSignals(
// parseInt(req.params.communityId),
// node.nodeDpid10,
// );
const engagements = await attestationService.getNodeEngagementSignals(node.nodeDpid10);

const verifiedEngagements = node.NodeAttestation.reduce(
Expand Down
66 changes: 66 additions & 0 deletions desci-server/src/scripts/backfill-radar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { prisma } from '../client.js';
import { logger } from '../logger.js';
import { communityService } from '../services/Communities.js';

const main = async () => {
const nodeAttestations = await prisma.nodeAttestation.findMany({ where: { revoked: false } });
let radarCount = 0;
for (const nodeAttestation of nodeAttestations) {
const exists = await prisma.communityRadarEntry.findFirst({
where: { nodeUuid: nodeAttestation.nodeUuid, desciCommunityId: nodeAttestation.desciCommunityId },
});
if (exists) {
logger.trace(
{
node: nodeAttestation.nodeDpid10,
communityId: nodeAttestation.desciCommunityId,
},
'Radar exists',
);
continue;
}

// check if node has claimed all community entry attestations
const entryAttestations = await communityService.getEntryAttestations({
desciCommunityId: nodeAttestation.desciCommunityId,
});

const claimedAttestations = await prisma.nodeAttestation.findMany({
where: { desciCommunityId: nodeAttestation.desciCommunityId, nodeUuid: nodeAttestation.nodeUuid },
});

const isEntriesClaimed = entryAttestations.every((entry) =>
claimedAttestations.find(
(claimed) =>
claimed.attestationId === entry.attestationId && claimed.attestationVersionId === entry.attestationVersionId,
),
);
if (!isEntriesClaimed) {
logger.info(
{
node: nodeAttestation.nodeDpid10,
claims: claimedAttestations.length,
entryAttestations: entryAttestations.length,
communityId: nodeAttestation.desciCommunityId,
},
'Not Qualified for Radar',
);
continue;
}
// End check if node has claimed all community entry attestations

await prisma.communityRadarEntry.create({
data: {
desciCommunityId: nodeAttestation.desciCommunityId,
nodeUuid: nodeAttestation.nodeUuid,
},
});
radarCount++;
}
logger.info({ radarCount }, 'Community radar fields: ');
return radarCount;
};

main()
.then((result) => console.log('Community backfilled', result))
.catch((err) => console.log('Error running script ', err));
2 changes: 1 addition & 1 deletion desci-server/src/services/Attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class AttestationService {
}

async getAttestationVersions(attestationId: number) {
return prisma.attestationVersion.findMany({ where: { attestationId } });
return prisma.attestationVersion.findMany({ where: { attestationId }, orderBy: { createdAt: 'desc' } });
}

async getAttestationVersion(id: number, attestationId: number) {
Expand Down
27 changes: 27 additions & 0 deletions desci-server/src/services/Communities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,33 @@ export class CommunityService {
async removeMemberById(id: number) {
return prisma.communityMember.delete({ where: { id } });
}

async addToRadar(desciCommunityId: number, nodeUuid: string) {
// check if node has claimed all community entry attestations
const entryAttestations = await communityService.getEntryAttestations({
desciCommunityId,
});

const claimedAttestations = await prisma.nodeAttestation.findMany({
where: { desciCommunityId, nodeUuid },
});

const isEntriesClaimed = entryAttestations.every((entry) =>
claimedAttestations.find(
(claimed) =>
claimed.attestationId === entry.attestationId && claimed.attestationVersionId === entry.attestationVersionId,
),
);

if (!isEntriesClaimed) return undefined;

return await prisma.communityRadarEntry.create({
data: {
desciCommunityId,
nodeUuid,
},
});
}
}

export const communityService = new CommunityService();

0 comments on commit 26cd763

Please sign in to comment.