Skip to content

Commit

Permalink
fix(chore): fix sonar code smells (#122)
Browse files Browse the repository at this point in the history
* fix(chore): fixing sonar smells

resolving sonar smells to improve quality gate

GH-111

* fix(chore): fix sonar code smells

Resolving sonar code smells to improve quality gate

GH-111

* fix(chore): fix sonar code smells

Resolving sonar smells to improve quality gate

GH-111

---------

Co-authored-by: Raghav <Raghav.arora@sourcefuse.com>
  • Loading branch information
arpit1503khanna and RaghavaroraSF committed May 16, 2023
1 parent 2a7eec3 commit af2ad32
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 55 deletions.
12 changes: 12 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Path to sources
sonar.sources=src
sonar.exclusions=src/__tests__/**
#sonar.inclusions=
# Path to tests
sonar.tests=src/__tests__
#sonar.test.exclusions=
#sonar.test.inclusions=
# Source encoding
sonar.sourceEncoding=UTF-8
# Exclusions for copy-paste detection
#sonar.cpd.exclusions=
17 changes: 10 additions & 7 deletions src/providers/push/apns/apns.provider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {inject, Provider} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {HttpErrors} from '@loopback/rest';
import apns from '@parse/node-apn';
import {ApnsBinding} from './keys';
import {ApnsConfigType, ApnsMessage, ApnsSubscriberType} from './types';
// sonarignore:start
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class ApnsProvider implements Provider<any> {
// sonarignore:end

export class ApnsProvider implements Provider<AnyObject> {
constructor(
@inject(ApnsBinding.Config, {
optional: true,
Expand Down Expand Up @@ -37,7 +36,8 @@ export class ApnsProvider implements Provider<any> {
);
}

if (message.receiver.to.length > 500) {
const maxReceivers = 500;
if (message.receiver.to.length > maxReceivers) {
throw new HttpErrors.BadRequest(
'Message receiver count cannot exceed 500 !',
);
Expand All @@ -47,9 +47,12 @@ export class ApnsProvider implements Provider<any> {
}
}
getMainNote(message: ApnsMessage) {
const expiresIn = 3600; // seconds
const floor = 1000;
const defaultBadgeCount = 3;
const note = new apns.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = this.apnsConfig?.options.badge ?? 3;
note.expiry = Math.floor(Date.now() / floor) + expiresIn; // Expires 1 hour from now.
note.badge = this.apnsConfig?.options.badge ?? defaultBadgeCount;
note.alert = message.body;
note.payload = {messageFrom: message.options.messageFrom};
// The topic is usually the bundle identifier of your application.
Expand Down
8 changes: 2 additions & 6 deletions src/providers/push/apns/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {AnyObject} from '@loopback/repository';
import {ProviderOptions} from '@parse/node-apn';
import {
PushMessage,
Expand All @@ -20,12 +21,7 @@ export interface ApnsConfigType {

export interface ApnsMessage extends PushMessage {
receiver: ApnsReceiver;
// sonarignore:start
options: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
};
// sonarignore:end
options: AnyObject;
}
export interface ApnsReceiver extends PushReceiver {
to: ApnsSubscriber[];
Expand Down
35 changes: 18 additions & 17 deletions src/providers/push/fcm/fcm.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class FcmProvider implements Provider<FcmNotification> {
fcmService: admin.app.App;

initialValidations(message: FcmMessage) {
const maxReceivers = 500;
if (
message.receiver.to.length === 0 &&
!message.options.topic &&
Expand All @@ -31,8 +32,7 @@ export class FcmProvider implements Provider<FcmNotification> {
'Message receiver, topic or condition not found in request !',
);
}

if (message.receiver.to.length > 500) {
if (message.receiver.to.length > maxReceivers) {
throw new HttpErrors.BadRequest(
'Message receiver count cannot exceed 500 !',
);
Expand Down Expand Up @@ -69,11 +69,12 @@ export class FcmProvider implements Provider<FcmNotification> {
...generalMessageObj,
data: {...message.options.data},
};
promises.push(
this.fcmService
.messaging()
.sendMulticast(msgToTransfer, (message.options.dryRun = false)),
);

const dryRun = message.options.dryRun ?? false;
const sendPromise = this.fcmService
.messaging()
.sendMulticast(msgToTransfer, dryRun);
promises.push(sendPromise);
}
return promises;
}
Expand All @@ -94,11 +95,11 @@ export class FcmProvider implements Provider<FcmNotification> {
data: {...message.options.data},
};

promises.push(
this.fcmService
.messaging()
.send(msgToTransfer, (message.options.dryRun = false)),
);
const dryRun = message.options.dryRun ?? false;
const sendPromise = this.fcmService
.messaging()
.send(msgToTransfer, dryRun);
promises.push(sendPromise);
});
}

Expand All @@ -123,11 +124,11 @@ export class FcmProvider implements Provider<FcmNotification> {
...generalMessageObj,
data: {...message.options.data},
};
promises.push(
this.fcmService
.messaging()
.send(msgToTransfer, (message.options.dryRun = false)),
);
const dryRun = message.options.dryRun ?? false;
const sendPromise = this.fcmService
.messaging()
.send(msgToTransfer, dryRun);
promises.push(sendPromise);
});
}

Expand Down
6 changes: 2 additions & 4 deletions src/providers/push/fcm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ export interface FcmMessage extends PushMessage {
webpush?: admin.messaging.WebpushConfig;
apns?: admin.messaging.ApnsConfig;
fcmOptions?: admin.messaging.FcmOptions;
// sonarignore:start
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
// sonarignore:end
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
[key: string]: any; //NOSONAR
};
}

Expand Down
12 changes: 6 additions & 6 deletions src/providers/push/pubnub/pubnub.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import {inject, Provider} from '@loopback/core';
import {HttpErrors} from '@loopback/rest';
import Pubnub from 'pubnub';
import {Aps, MessageConfig, PnApns, TargetsType} from '.';
import {PayloadType} from './types';
import {Config} from '../../../types';
import {PubnubBindings} from './keys';
import {PubNubMessage, PubNubNotification, PubNubSubscriberType} from './types';
import {
PayloadType,
PubNubMessage,
PubNubNotification,
PubNubSubscriberType,
} from './types';

export class PubNubProvider implements Provider<PubNubNotification> {
constructor(
Expand Down Expand Up @@ -56,10 +60,6 @@ export class PubNubProvider implements Provider<PubNubNotification> {
aps: apsData,
pnPush: targetTypeData,
};
// const generalMessageObj = {
// pn_gcm: pn_gcm,
// pn_apns: Object.assign(pn_apns, message.options),
// };
return {pnGcm: pnGcm, pnApns: Object.assign(pnApns, message.options)};
}
getPublishConfig(message: PubNubMessage) {
Expand Down
8 changes: 2 additions & 6 deletions src/providers/push/socketio/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {AnyObject} from '@loopback/repository';
import {
PushMessage,
PushNotification,
Expand Down Expand Up @@ -32,10 +33,5 @@ export interface SocketConfig {
* Path represents the default socket server endpoint
*/
defaultPath: string;
options: {
// sonarignore:start
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
// sonarignore:end
};
options: AnyObject;
}
12 changes: 3 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {AnyObject} from '@loopback/repository';
export interface INotification {
publish(message: Message): Promise<void>;
}
Expand All @@ -22,20 +23,13 @@ export interface Config {
options?: MessageOptions;
}

export interface MessageOptions {
// sonarignore:start
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
// sonarignore:end
}
export type MessageOptions = AnyObject;

export interface Subscriber {
id: string;
name?: string;
// sonarignore:start
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
// sonarignore:end
[key: string]: any; //NOSONAR
}

export interface Receiver {
Expand Down

0 comments on commit af2ad32

Please sign in to comment.