Skip to content

Commit

Permalink
[Moments] - Add cid field and patch request (#117)
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
nicolasdeleon authored May 9, 2024
1 parent 023c577 commit 35a7e55
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 25 deletions.
1 change: 1 addition & 0 deletions docs/pages/packages/moments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Features

- Create a Moment attached to a Drop or a specific POAP
- Update a Moment
- Fetch multiple Moments
- Fetch a single Moment

Expand Down
3 changes: 3 additions & 0 deletions examples/moments/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AuthenticationProviderHttp,
} from '@poap-xyz/providers';
import { create_moment } from './methods/create_moment';
import { patch_moment } from './methods/patch_moment';
import { fetch_multiple_moments } from './methods/fetch_multiple_moments';
import { fetch_single_moment } from './methods/fetch_single_moment';
import { fetch_moments_by_drop_ids } from './methods/fetch_moments_by_drop_ids';
Expand Down Expand Up @@ -38,6 +39,8 @@ async function main(): Promise<void> {
await fetch_single_moment(client);
// Fetch moments by drop ids
await fetch_moments_by_drop_ids(client);
// Patch Moment
await patch_moment(client);
}

main().catch(() => {
Expand Down
15 changes: 15 additions & 0 deletions examples/moments/backend/src/methods/patch_moment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MomentsClient, PatchMomentInput } from '@poap-xyz/moments';

export const patch_moment = async (client: MomentsClient): Promise<void> => {
const momentId = "b08fad41-7154-499f-88f9-abea66ceab48"
const input: PatchMomentInput = {
cid: "0001-7ce5368171cc3d988157d7dab3d313d7bd43de3e-365e5b83699adce0825021d011f1bf73bd5ef9369d06e49645afbea2ef34f54e0557c1d4742c8bd6d1f7a02be4aa483c03888af0aa143d5aa7351e2baaf931231c.moment",
};

try {
await client.patchMoment(momentId, input);
console.log('Patch successful');
} catch (error) {
console.log(error);
}
};
6 changes: 3 additions & 3 deletions packages/drops/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/drops",
"version": "0.2.6",
"version": "0.2.7",
"description": "Drops module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down Expand Up @@ -29,7 +29,7 @@
"node": ">=18"
},
"dependencies": {
"@poap-xyz/providers": "0.2.6",
"@poap-xyz/utils": "0.2.6"
"@poap-xyz/providers": "0.2.7",
"@poap-xyz/utils": "0.2.7"
}
}
6 changes: 3 additions & 3 deletions packages/moments/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/moments",
"version": "0.2.6",
"version": "0.2.7",
"description": "Moments module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,8 +26,8 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.2.6",
"@poap-xyz/utils": "0.2.6",
"@poap-xyz/providers": "0.2.7",
"@poap-xyz/utils": "0.2.7",
"uuid": "^9.0.0"
},
"engines": {
Expand Down
24 changes: 24 additions & 0 deletions packages/moments/src/client/MomentsClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MomentsClient } from './MomentsClient';
import { CreateMomentInput } from './dtos/create/CreateInput';
import { CreateSteps } from './dtos/create/CreateSteps';
import { v4 } from 'uuid';
import { PatchMomentInput } from './dtos/patch/PatchInput';

describe('MomentsClient', () => {
const MOMENT_ID = 'this-is-a-moment-id';
Expand All @@ -16,6 +17,8 @@ describe('MomentsClient', () => {
const FILE_2_TYPE = 'image/jpeg';
const MEDIA_UPLOAD_URL = 'this-is-a-media-upload-url';
const DESCRIPTION = 'This is a description';
const MOMENT_CID =
'0001-7ce5368171cc3d988157d7dab3d313d7bd43de3e-365e5b83699adce0825021d011f1bf73bd5ef9369d06e49645afbea2ef34f54e0557c1d4742c8bd6d1f7a02be4aa483c03888af0aa143d5aa7351e2baaf931231c.moment';
const MEDIAS_TO_CREATE = [
{
fileBinary: FILE_1,
Expand Down Expand Up @@ -108,4 +111,25 @@ describe('MomentsClient', () => {
expect(onStepUpdate).toHaveBeenCalledTimes(4);
});
});
describe('updateMoment', () => {
it('should update a moment', async () => {
// GIVEN
const client = new MomentsClient(
poapMomentsAPIMocked,
compassProviderMocked,
);
const input: PatchMomentInput = {
cid: MOMENT_CID,
};
poapMomentsAPIMocked.patchMoment.mockResolvedValue();

// WHEN
await client.patchMoment(MOMENT_ID, input);

// THEN
expect(poapMomentsAPIMocked.patchMoment).toHaveBeenCalledWith(MOMENT_ID, {
cid: MOMENT_CID,
});
});
});
});
7 changes: 7 additions & 0 deletions packages/moments/src/client/MomentsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
MomentsSortFields,
} from './dtos/fetch/FetchMomentsInput';
import { CreateMedia } from './dtos/create/CreateMedia';
import { PatchMomentInput } from './dtos/patch/PatchInput';

export class MomentsClient {
constructor(
Expand Down Expand Up @@ -65,6 +66,7 @@ export class MomentsClient {
response.dropId,
response.tokenId,
response.description,
response.cid,
);
}

Expand Down Expand Up @@ -196,6 +198,10 @@ export class MomentsClient {
return result;
}

public async patchMoment(id: string, input: PatchMomentInput): Promise<void> {
await this.poapMomentsApi.patchMoment(id, input);
}

private getMomentFromMomentResponse(momentResponse: MomentResponse): Moment {
return new Moment(
momentResponse.id,
Expand All @@ -204,6 +210,7 @@ export class MomentsClient {
momentResponse.drop_id,
momentResponse.token_id,
momentResponse.description,
momentResponse.cid,
);
}
}
8 changes: 8 additions & 0 deletions packages/moments/src/client/dtos/patch/PatchInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Interface representing the input needed to patch a moment.
* @interface
* @property {string} cid - The cid of the moment in Registry.
*/
export interface PatchMomentInput {
cid?: string;
}
7 changes: 7 additions & 0 deletions packages/moments/src/domain/Moment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,26 @@ export class Moment {
*/
public readonly description?: string;

/**
* The cid of the moment in Registry
*/
public readonly cid?: string;

constructor(
id: string,
author: string,
createdOn: Date,
dropId: number,
tokenId?: number,
description?: string,
cid?: string,
) {
this.id = id;
this.author = author;
this.createdOn = createdOn;
this.dropId = dropId;
this.tokenId = tokenId;
this.description = description;
this.cid = cid;
}
}
1 change: 1 addition & 0 deletions packages/moments/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { Moment } from './domain/Moment';
export { MomentsClient } from './client/MomentsClient';
export { CreateMomentInput } from './client/dtos/create/CreateInput';
export { PatchMomentInput } from './client/dtos/patch/PatchInput';
export { CreateSteps } from './client/dtos/create/CreateSteps';
export { FetchMomentsInput } from './client/dtos/fetch/FetchMomentsInput';
export { MomentsClientFactory } from './client/MomentsClientFactory/MomentsClientFactory';
6 changes: 2 additions & 4 deletions packages/moments/src/queries/PaginatedMoments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ export const PAGINATED_MOMENTS_QUERY = `
created_on
drop_id
id
gateways
media_key
token_id
description
cid
}
}
`;
Expand All @@ -29,10 +28,9 @@ export interface MomentResponse {
created_on: string;
drop_id: number;
id: string;
gateways: string[];
media_key: string;
token_id: number;
description?: string;
cid?: string;
}

export interface MomentsQueryResponse {
Expand Down
6 changes: 3 additions & 3 deletions packages/poaps/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poaps",
"version": "0.2.6",
"version": "0.2.7",
"description": "Poaps module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,8 +26,8 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.2.6",
"@poap-xyz/utils": "0.2.6"
"@poap-xyz/providers": "0.2.7",
"@poap-xyz/utils": "0.2.7"
},
"engines": {
"node": ">=18"
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/providers",
"version": "0.2.6",
"version": "0.2.7",
"description": "Providers module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,7 +26,7 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/utils": "0.2.6",
"@poap-xyz/utils": "0.2.7",
"axios": "^1.3.5",
"lodash.chunk": "^4.2.0"
},
Expand Down
17 changes: 17 additions & 0 deletions packages/providers/src/core/PoapMomentsApi/PoapMomentsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { AxiosError } from 'axios';
import { InvalidMediaError } from '../../ports/MomentsApiProvider/errors/InvalidMediaError';
import { CreateMomentResponse } from '../../ports/MomentsApiProvider/types/CreateMomentResponse';
import { CreateMomentInput } from '../../ports/MomentsApiProvider/types/CreateMomentInput';
import { PatchMomentInput } from '../../ports/MomentsApiProvider/types/PatchMomentInput';
import { MomentsApiProvider } from '../../ports/MomentsApiProvider/MomentsApiProvider';
import { AuthenticationProvider } from '../../ports/AuthenticationProvider/AuthenticationProvider';
import { MediaStatus } from '../../ports/MomentsApiProvider/types/MediaStatus';
Expand Down Expand Up @@ -164,6 +165,22 @@ export class PoapMomentsApi implements MomentsApiProvider {
}
}

/**
* Update a moment using the provided input
* @param {string} id - The moment id
* @param {PatchMomentInput} input - The input for updating a moment
*/
public async patchMoment(id: string, input: PatchMomentInput): Promise<void> {
await fetch(`${this.baseUrl}/moments/${id}`, {
method: 'PATCH',
body: JSON.stringify(input),
headers: {
'Content-Type': 'application/json',
Authorization: await this.getAuthorizationToken(),
},
});
}

private async getAuthorizationToken(): Promise<string> {
if (!this.authenticationProvider) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CreateMomentInput } from './types/CreateMomentInput';
import { CreateMomentResponse } from './types/CreateMomentResponse';
import { PatchMomentInput } from './types/PatchMomentInput';
import { MediaStatus } from './types/MediaStatus';

/**
Expand Down Expand Up @@ -55,4 +56,14 @@ export interface MomentsApiProvider {
* @returns {Promise<MediaStatus>} - A Promise that resolves with the media processing status
*/
fetchMediaStatus(mediaKey: string): Promise<MediaStatus>;

/**
* Updates a moment on the API.
* @async
* @function
* @name MomentsApiProvider#patchMoment
* @param {string} id - The Moment id.
* @param {PatchMomentInput} input - The input data for updating a moment.
*/
patchMoment(id: string, input: PatchMomentInput): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ export interface CreateMomentResponse {
* The description of the moment.
*/
description?: string;

/**
* The cid of the moment in Registry.
*/
cid?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Object describing the input required to update a Moment.
* @typedef {Object} PatchMomentInput
* @property {string} cid - The cid of the moment in Registry under the format:
* {<Version>-<Signer>-<Signature>.<Type>} allows you to fetch and verify the
* content of the moment in Registry.
*/
export interface PatchMomentInput {
cid?: string;
}
40 changes: 40 additions & 0 deletions packages/providers/test/PoapMomentsApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { InvalidMediaError } from '../src/ports/MomentsApiProvider/errors/Invali
import { mock, MockProxy } from 'jest-mock-extended';
import { MediaStatus } from '../src/ports/MomentsApiProvider/types/MediaStatus';
import { CreateMomentInput } from '../src/ports/MomentsApiProvider/types/CreateMomentInput';
import { enableFetchMocks } from 'jest-fetch-mock';

enableFetchMocks();

describe('PoapMomentsApi', () => {
const BASE_URL = 'https://moments.test';
Expand Down Expand Up @@ -131,4 +134,41 @@ describe('PoapMomentsApi', () => {
await expect(api.createMoment(createMomentInput)).rejects.toThrow();
});
});

describe('patchMoment', () => {
const id = '1';
const patchMomentInput = {
cid: '0001-7ce5368171cc3d988157d7dab3d313d7bd43de3e-365e5b83699adce0825021d011f1bf73bd5ef9369d06e49645afbea2ef34f54e0557c1d4742c8bd6d1f7a02be4aa483c03888af0aa143d5aa7351e2baaf931231c.moment',
};

it('should call fetch with correct parameters for PATCH request', async () => {
fetchMock.mockOnce(() =>
Promise.resolve({
ok: true,
status: 200,
body: JSON.stringify({}),
}),
);

const result = await api.patchMoment(id, patchMomentInput);

expect(result).toBe(undefined);
expect(fetch).toHaveBeenCalledWith(
`${BASE_URL}/moments/${id}`,
{
method: 'PATCH',
body: JSON.stringify(patchMomentInput),
headers: {
'Content-Type': 'application/json',
Authorization: expect.any(String),
},
}
);
});

it('should throw error when AuthenticationProvider is not provided', async () => {
api = new PoapMomentsApi({});
await expect(api.patchMoment(id, patchMomentInput)).rejects.toThrow();
});
});
});
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/utils",
"version": "0.2.6",
"version": "0.2.7",
"description": "Utils module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
Loading

0 comments on commit 35a7e55

Please sign in to comment.