Skip to content

Commit

Permalink
Merge pull request #746 from desci-labs/fix-attestation
Browse files Browse the repository at this point in the history
Optional attestation publish during update
  • Loading branch information
shadrach-tayo authored Jan 7, 2025
2 parents 69c7bf2 + c8ad5fb commit 50372ee
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
29 changes: 17 additions & 12 deletions desci-server/src/controllers/admin/communities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ export const createAttestation = async (req: Request, res: Response, _next: Next
export const updateAttestation = async (req: Request, res: Response, _next: NextFunction) => {
const body = req.body as Required<z.infer<typeof addAttestationSchema>['body']>;
const { attestationId } = req.params as z.infer<typeof updateAttestationSchema>['params'];
logger.info({ attestationId, body }, 'Payload');
const { publishNew } = req.body as z.infer<typeof updateAttestationSchema>['body'];
logger.info({ attestationId, publishNew, body }, 'Payload');

const exists = await attestationService.findAttestationById(Number(attestationId));
if (!exists) throw new NotFoundError(`Attestation ${attestationId} not found`);
Expand Down Expand Up @@ -311,22 +312,26 @@ export const updateAttestation = async (req: Request, res: Response, _next: Next
delete body.imageUrl;
delete body.verifiedImageUrl;

// logger.info({ image_url, verified_image_url }, 'Assets');

if (!image_url) throw new BadRequestError('No attestation image uploaded');

const isProtected = body.protected.toString() === 'true' ? true : false;
const doiPrivilege = body.canMintDoi.toString() === 'true' ? true : false;
const orcidPrivilege = body.canUpdateOrcid.toString() === 'true' ? true : false;
const attestation = await attestationService.updateAttestation(exists.id, {
...body,
image_url,
verified_image_url,
communityId: exists.communityId,
protected: isProtected,
canMintDoi: doiPrivilege,
canUpdateOrcid: orcidPrivilege,
});
logger.trace({ publishNew }, 'PUBLISH NEW');
const publishNewVersion = publishNew?.toString() == 'false' ? false : true;
const attestation = await attestationService.updateAttestation(
exists.id,
{
...body,
image_url,
verified_image_url,
communityId: exists.communityId,
protected: isProtected,
canMintDoi: doiPrivilege,
canUpdateOrcid: orcidPrivilege,
},
publishNewVersion,
);
new SuccessResponse(attestation).send(res);
};

Expand Down
1 change: 1 addition & 0 deletions desci-server/src/routes/v1/admin/communities/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const addAttestationSchema = z.object({

export const updateAttestationSchema = addAttestationSchema.extend({
params: z.object({ attestationId: z.coerce.number(), communityId: z.coerce.number() }),
body: z.object({ publishNew: z.coerce.boolean().optional() }),
});

export const addMemberSchema = z.object({
Expand Down
30 changes: 22 additions & 8 deletions desci-server/src/services/Attestation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';

import { HighlightBlock } from '@desci-labs/desci-models';
import { AnnotationType, Attestation, Node, Prisma, User } from '@prisma/client';
import { AnnotationType, Attestation, AttestationVersion, Node, Prisma, User } from '@prisma/client';
import sgMail from '@sendgrid/mail';
import _ from 'lodash';

Expand Down Expand Up @@ -181,7 +181,7 @@ export class AttestationService {
return prisma.attestationTemplate.create({ data: template });
}

async updateAttestation(attestationId: number, data: Prisma.AttestationUncheckedCreateInput) {
async updateAttestation(attestationId: number, data: Prisma.AttestationUncheckedCreateInput, publishNew = true) {
const attestation = await this.findAttestationById(attestationId);

if (!attestation) throw new AttestationNotFoundError();
Expand All @@ -194,12 +194,26 @@ export class AttestationService {
canUpdateOrcid: data.canUpdateOrcid,
},
});
const attestationVersion = await this.#publishVersion({
name: data.name as string,
description: data.description,
image_url: data.image_url,
attestationId: attestation.id,
});

let attestationVersion: AttestationVersion;

if (publishNew) {
attestationVersion = await this.#publishVersion({
name: data.name as string,
description: data.description,
image_url: data.image_url,
attestationId: attestation.id,
});
} else {
const latestVersion = await prisma.attestationVersion.findFirst({
where: { attestationId: attestation.id },
orderBy: { createdAt: 'desc' },
});
attestationVersion = await prisma.attestationVersion.update({
where: { id: latestVersion.id },
data: { name: data.name as string, description: data.description, image_url: data.image_url },
});
}

const communityEntryAttestation = await prisma.communityEntryAttestation.findFirst({
where: { attestationId: attestation.id },
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ services:
condition: service_healthy
nodes_test_ipfs:
condition: service_started
nodes_test_repo_service:
condition: service_healthy
# nodes_test_repo_service:
# condition: service_healthy
links:
- nodes_test_db
container_name: "nodes_backend_test"
Expand Down

0 comments on commit 50372ee

Please sign in to comment.