Skip to content

Commit

Permalink
Fix/get subscriber preference channel update (#7172)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawan Jain <[email protected]>
  • Loading branch information
SokratisVidros and jainpawan21 authored Nov 29, 2024
1 parent d5dab39 commit 84883f8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
24 changes: 22 additions & 2 deletions packages/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,22 @@ await novu.subscribers.updateOnlineStatus('subscriberId', false);

- #### Get subscriber preference for all workflows

This method returns subscriber preference for all workflows with inactive channels by default. To get subscriber preference for all workflows without inactive (means only active) channels, pass `false` as second argument.

```ts
import { Novu } from '@novu/node';

const novu = new Novu('<NOVU_SECRET_KEY>');

await novu.subscribers.getPreference('subscriberId');
// return subscriber preference for all workflows without inactive channels
await novu.subscribers.getPreference('subscriberId', {
includeInactiveChannels: false,
});

// return subscriber preference for all workflows with inactive channels
await novu.subscribers.getPreference('subscriberId', {
includeInactiveChannels: true,
});
```

- #### Get subscriber global preference
Expand Down Expand Up @@ -756,13 +766,23 @@ await novu.messages.list(params);
- #### Delete a message by `messageId`

```ts
import { Novu, ChannelTypeEnum } from '@novu/node';
import { Novu } from '@novu/node';

const novu = new Novu('<NOVU_SECRET_KEY>');

await novu.messages.deleteById('messageId');
```

- #### Delete multiple messages by `transactionId`

```ts
import { Novu } from '@novu/node';

const novu = new Novu('<NOVU_SECRET_KEY>');

await novu.messages.deleteByTransactionId('transactionId');
```

### Layouts

- #### Create a layout
Expand Down
11 changes: 4 additions & 7 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
"build": "run-p build:*",
"build:main": "tsc -p tsconfig.json",
"build:module": "tsc -p tsconfig.module.json",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"lint:fix": "eslint src --fix",
"test": "run-s test:*",
"lint": "eslint src",
"test:prettier": "prettier \"src/**/*.ts\"",
"test:unit": "jest src",
"test": "jest src",
"lint": "run-s lint:*",
"lint:eslint": "eslint src --fix",
"lint:prettier": "prettier \"src/**/*.ts\" --write",
"check-cli": "run-s test diff-integration-tests check-integration-tests",
"check-integration-tests": "run-s check-integration-test:*",
"diff-integration-tests": "mkdir -p diff && rm -rf diff/test && cp -r test diff/test && rm -rf diff/test/test-*/.git && cd diff && git init --quiet && git add -A && git commit --quiet --no-verify --allow-empty -m 'WIP' && echo '\\n\\nCommitted most recent integration test output in the \"diff\" directory. Review the changes with \"cd diff && git diff HEAD\" or your preferred git diff viewer.'",
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/lib/retry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Novu Node.js package - Retries and idempotency-key', () => {
nock(BACKEND_URL)
.post(TRIGGER_PATH)
.times(3)
.reply(function (_url, _body) {
.reply(function cb(_url, _body) {
idempotencyKeys.push(this.req.getHeader('idempotency-key') as string);

return [500, { message: 'Server Exception' }];
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('Novu Node.js package - Retries and idempotency-key', () => {
nock(BACKEND_URL)
.post(TRIGGER_PATH)
.times(3)
.reply(function () {
.reply(function cb() {
idempotencyKeys.push(this.req.getHeader('idempotency-key') as string);

return [422, { message: 'Unprocessable Content' }];
Expand Down
5 changes: 4 additions & 1 deletion packages/node/src/lib/subscribers/subscriber.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export interface ISubscribers {
*/
unsetCredentials(subscriberId: string, providerId: string);
updateOnlineStatus(subscriberId: string, online: boolean);
getPreference(subscriberId: string);
getPreference(
subscriberId: string,
{ includeInactiveChannels }: { includeInactiveChannels: boolean },
);
getGlobalPreference(subscriberId: string);
getPreferenceByLevel(subscriberId: string, level: PreferenceLevelEnum);
updatePreference(
Expand Down
13 changes: 11 additions & 2 deletions packages/node/src/lib/subscribers/subscribers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,19 @@ describe('test use of novus node package - Subscribers class', () => {

await novu.subscribers.getPreference('test-subscriber-preference');

expect(mockedAxios.get).toHaveBeenCalled();
expect(mockedAxios.get).toHaveBeenCalledWith(
expect(mockedAxios.get).toHaveBeenNthCalledWith(
1,
'/subscribers/test-subscriber-preference/preferences',
);

await novu.subscribers.getPreference('test-subscriber-preference', {
includeInactiveChannels: true,
});

expect(mockedAxios.get).toHaveBeenNthCalledWith(
2,
'/subscribers/test-subscriber-preference/preferences?includeInactiveChannels=true',
);
});

test('should update subscriber preference', async () => {
Expand Down
21 changes: 19 additions & 2 deletions packages/node/src/lib/subscribers/subscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,25 @@ export class Subscribers extends WithHttp implements ISubscribers {
return await this.http.delete(`/subscribers/${subscriberId}`);
}

async getPreference(subscriberId: string) {
return await this.http.get(`/subscribers/${subscriberId}/preferences`);
async getPreference(
subscriberId: string,
options: { includeInactiveChannels?: boolean } = {},
) {
const { includeInactiveChannels } = options;
const searchParams = new URLSearchParams();

if (includeInactiveChannels) {
searchParams.append('includeInactiveChannels', 'true');
}

const searchParamsString = searchParams.toString();

let url = `/subscribers/${subscriberId}/preferences`;
if (searchParamsString) {
url += `?${searchParamsString}`;
}

return await this.http.get(url);
}

async getGlobalPreference(subscriberId: string) {
Expand Down

0 comments on commit 84883f8

Please sign in to comment.