Skip to content

Commit

Permalink
Merge pull request #222 from d-rec/DR-767
Browse files Browse the repository at this point in the history
DR-767 Convert code base to type script
  • Loading branch information
Aish1990 authored May 13, 2024
2 parents 5a51bbb + f22c158 commit b20f34b
Show file tree
Hide file tree
Showing 39 changed files with 454 additions and 632 deletions.
6 changes: 3 additions & 3 deletions apps/drec-api/src/auth/client-jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export class ClientJwtStrategy extends PassportStrategy(
}

async validate(request: Request, payload: IJWTPayload) {
// @ts-ignore
const token = request.headers.authorization?.split(' ')[1];
const token = (
request.headers as { authorization?: string }
).authorization?.split(' ')[1];
const user = await this.userService.findByEmail(payload.email);
// @ts-ignore
const publicKey = this.oauthClientService.get(user.api_user_id);
const verifiedData = await this.jwtService.verify(token, {
publicKey: (await publicKey).client_id,
Expand Down
5 changes: 3 additions & 2 deletions apps/drec-api/src/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
payload: IJWTPayload,
): Promise<IUser | null> {
this.logger.verbose('With in validate');
// @ts-ignore
const token = request.headers.authorization?.split(' ')[1];
const token = (
request.headers as { authorization?: string }
).authorization?.split(' ')[1];
const tokeninvalidate = await this.authService.isTokenBlacklisted(
token,
payload,
Expand Down
17 changes: 7 additions & 10 deletions apps/drec-api/src/guards/WithoutAuthGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@ export class WithoutAuthGuard implements CanActivate {
})
).user;
} else if (request.url.split('/')[3] === 'register') {
const userData = await this.userService.findOne({ role: Role.Admin });
if (
!request.body.api_user_id &&
request.body.api_user_id == undefined &&
(request.body.organizationType === 'Developer' ||
request.body.organizationType === Role.Buyer)
) {
user = await this.userService.findOne({ role: Role.Admin });
user = userData;
} else if (
request.body.api_user_id &&
request.body.api_user_id !=
// @ts-ignore ts(2339)
(await this.userService.findOne({ role: Role.Admin }).api_user_id) &&
request.body.api_user_id != undefined &&
request.body.api_user_id != userData.api_user_id &&
(request.body.organizationType === 'Developer' ||
request.body.organizationType === Role.Buyer)
) {
Expand Down Expand Up @@ -82,11 +81,9 @@ export class WithoutAuthGuard implements CanActivate {
request.body.organizationType === undefined &&
user.role != Role.Admin &&
user.role != Role.ApiUser &&
// @ts-ignore ts(2339)
user.api_user_id !=
// prettier-ignore
// @ts-ignore ts(2339)
((await this.userService.findOne({ role: Role.Admin })) as IUser).api_user_id
((await this.userService.findOne({ role: Role.Admin })) as IUser)
.api_user_id
) {
throw new UnauthorizedException({
statusCode: 401,
Expand Down
2 changes: 0 additions & 2 deletions apps/drec-api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { startAPI } from '.';
import { Logger } from '@nestjs/common';

// @ts-ignore
process.setMaxListeners(0);

const logger = new Logger();

// @ts-ignore
process.on('warning', (e) => {
logger.warn('warning inside main.ts');
logger.warn(e.stack);
Expand Down
1 change: 0 additions & 1 deletion apps/drec-api/src/models/LoggedInUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export class LoggedInUser implements ILoggedInUser {
this.blockchainAccountAddress = user.organization?.blockchainAccountAddress;
this.role = user.role;
this.permissions = user.permissions;
// @ts-ignore
this.api_user_id = user.api_user_id;
}

Expand Down
9 changes: 8 additions & 1 deletion apps/drec-api/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { IEmailConfirmation, IFullOrganization } from '.';
import { Role, UserStatus, PermissionString } from '../utils/enums';
import {
Role,
UserStatus,
PermissionString,
UserPermissionStatus,
} from '../utils/enums';
import { IModulePermissionsConfig } from './ACLModulesPermission';
export interface IUserProperties {
id: number;
Expand All @@ -13,6 +18,8 @@ export interface IUserProperties {
role: Role;
roleId?: number;
permissions?: PermissionString;
api_user_id?: string;
permission_status?: UserPermissionStatus;
}

export interface IUserSeed extends IUser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,41 @@ describe('CertificateLogService', () => {
it('should create CSV file and send it as response', async () => {
const mockData: CheckCertificateIssueDateLogForDeviceEntity[] = [
{
id: 1,
certificate_issuance_startdate: new Date('2023-11-06T12:48:18.405Z'),
certificate_issuance_enddate: new Date('2023-11-10T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '14f8bcd3-095b-4659-90d8-bbc7523d14b4',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 3,
},
groupId: 2,
status: 'Active',
ongoing_start_date: '2023-11-06T12:48:18.405Z',
ongoing_end_date: '2023-11-10T04:15:58.000Z',
} as CheckCertificateIssueDateLogForDeviceEntity,
{
id: 2,
certificate_issuance_startdate: new Date('2023-11-06T12:48:18.405Z'),
certificate_issuance_enddate: new Date('2023-11-09T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '37aa312a-405d-4e37-97f3-8af06a0b1e10',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 2,
},
groupId: 3,
status: 'Active',
ongoing_start_date: '2023-11-06T12:48:18.405Z',
ongoing_end_date: '2023-11-10T04:15:58.000Z',
} as CheckCertificateIssueDateLogForDeviceEntity,
{
id: 3,
certificate_issuance_startdate: new Date('2022-11-26T11:01:00.000Z'),
certificate_issuance_enddate: new Date('2023-11-06T08:27:44.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '770d39fd-fbb3-4eb9-82df-260a740b5151',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 1,
},
groupId: 4,
status: 'Pending',
ongoing_start_date: '2023-11-06T12:48:18.405Z',
ongoing_end_date: '2023-11-10T04:15:58.000Z',
} as CheckCertificateIssueDateLogForDeviceEntity,
];

jest
Expand Down Expand Up @@ -175,35 +181,41 @@ describe('CertificateLogService', () => {

const expectedLogs: CheckCertificateIssueDateLogForDeviceEntity[] = [
{
id: 1,
groupId: 2,
certificate_issuance_startdate: new Date('2023-11-06T12:48:18.405Z'),
certificate_issuance_enddate: new Date('2023-11-10T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '14f8bcd3-095b-4659-90d8-bbc7523d14b4',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 3,
},
status: 'Active',
ongoing_start_date: '2023-11-06T12:48:18.405Z',
ongoing_end_date: '2023-11-10T04:15:58.000Z',
} as CheckCertificateIssueDateLogForDeviceEntity,
{
id: 2,
groupId: 3,
certificate_issuance_startdate: new Date('2023-11-06T12:48:18.405Z'),
certificate_issuance_enddate: new Date('2023-11-09T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '37aa312a-405d-4e37-97f3-8af06a0b1e10',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 2,
},
status: 'pending',
ongoing_start_date: '2023-11-06T12:48:18.405Z',
ongoing_end_date: '2023-11-09T04:15:58.000Z',
} as CheckCertificateIssueDateLogForDeviceEntity,
{
id: 3,
groupId: 4,
certificate_issuance_startdate: new Date('2022-11-26T11:01:00.000Z'),
certificate_issuance_enddate: new Date('2023-11-06T08:27:44.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '770d39fd-fbb3-4eb9-82df-260a740b5151',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 1,
},
status: 'pending',
ongoing_start_date: '2023-11-06T12:48:18.405Z',
ongoing_end_date: '2023-11-09T04:15:58.000Z',
} as CheckCertificateIssueDateLogForDeviceEntity,
];

jest
Expand Down Expand Up @@ -268,8 +280,10 @@ describe('CertificateLogService', () => {
},
ownerId: '',
hasOrganization: false,
// @ts-ignore
hasPermission: true,
permissions: PermissionString.Write,
hasPermission: function (...permissions: PermissionString[]): boolean {
throw new Error('Function not implemented.');
},
api_user_id: 'apiuserId',
};
const filterDto: FilterDTO = {
Expand Down Expand Up @@ -326,7 +340,6 @@ describe('CertificateLogService', () => {
certificate_issuance_enddate: new Date('2023-11-10T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '14f8bcd3-095b-4659-90d8-bbc7523d14b4',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 3,
Expand All @@ -338,7 +351,6 @@ describe('CertificateLogService', () => {
certificate_issuance_enddate: new Date('2023-11-09T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '37aa312a-405d-4e37-97f3-8af06a0b1e10',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 2,
Expand All @@ -350,7 +362,6 @@ describe('CertificateLogService', () => {
certificate_issuance_enddate: new Date('2023-11-06T08:27:44.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '770d39fd-fbb3-4eb9-82df-260a740b5151',
// @ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 1,
Expand Down Expand Up @@ -401,8 +412,10 @@ describe('CertificateLogService', () => {
},
ownerId: '',
hasOrganization: false,
// @ts-ignore
hasPermission: true,
permissions: PermissionString.Write,
hasPermission: function (...permissions: PermissionString[]): boolean {
throw new Error('Function not implemented.');
},
api_user_id: 'apiuserId',
};
const filterDto: FilterDTO = {
Expand Down Expand Up @@ -459,7 +472,6 @@ describe('CertificateLogService', () => {
certificate_issuance_enddate: new Date('2023-11-10T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '14f8bcd3-095b-4659-90d8-bbc7523d14b4',
//@ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 3,
Expand All @@ -471,7 +483,6 @@ describe('CertificateLogService', () => {
certificate_issuance_enddate: new Date('2023-11-09T04:15:58.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '37aa312a-405d-4e37-97f3-8af06a0b1e10',
//@ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 2,
Expand All @@ -483,7 +494,6 @@ describe('CertificateLogService', () => {
certificate_issuance_enddate: new Date('2023-11-06T08:27:44.000Z'),
readvalue_watthour: 10000,
certificateTransactionUID: '770d39fd-fbb3-4eb9-82df-260a740b5151',
//@ts-ignore
reservation_name: 'secondreservation',
externalId: 'Ext2',
blockchainCertificateId: 1,
Expand Down Expand Up @@ -523,8 +533,10 @@ describe('CertificateLogService', () => {
},
ownerId: '',
hasOrganization: false,
// @ts-ignore
hasPermission: true,
permissions: PermissionString.Write,
hasPermission: function (...permissions: PermissionString[]): boolean {
throw new Error('Function not implemented.');
},
api_user_id: 'apiuserId',
};
const filterDto: FilterDTO = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ export class CertificateLogService {
const totalPages = Math.ceil(total / itemsPerPage);

const request: IGetAllCertificatesOptions = {
// @ts-ignore
deviceId: parseInt(groupid),
deviceId: `${parseInt(groupid)}`,
};

const certifiedreservation1: ICertificateReadModel<ICertificateMetadata>[] =
Expand Down Expand Up @@ -261,14 +260,11 @@ export class CertificateLogService {
certifiedlist: ICertificateReadModel<ICertificateMetadata>,
index: number,
) => {
// @ts-ignore ts(2339)
certificatesInReservationWithLog[index].certificateStartDate =
new Date(certifiedlist.generationStartTime * 1000).toISOString();
// @ts-ignore ts(2339)
certificatesInReservationWithLog[index].certificateEndDate = new Date(
certifiedlist.generationEndTime * 1000,
).toISOString();
// @ts-ignore ts(2339)
certificatesInReservationWithLog[index].perDeviceCertificateLog = [];
try {
if (typeof certifiedlist.metadata === 'string') {
Expand Down Expand Up @@ -323,7 +319,6 @@ export class CertificateLogService {

certificatesInReservationWithLog[
index
// @ts-ignore ts(2339)
].perDeviceCertificateLog.push(singleDeviceLogEle);
});

Expand Down Expand Up @@ -844,15 +839,12 @@ export class CertificateLogService {
certifiedlist: ICertificateReadModel<ICertificateMetadata>,
index: number,
) => {
// @ts-ignore ts(2339)
certificatesInReservationWithLog[index].certificateStartDate =
new Date(
certifiedlist.generationStartTime * 1000,
).toISOString();
// @ts-ignore ts(2339)
certificatesInReservationWithLog[index].certificateEndDate =
new Date(certifiedlist.generationEndTime * 1000).toISOString();
// @ts-ignore ts(2339)
certificatesInReservationWithLog[index].perDeviceCertificateLog =
[];
try {
Expand Down Expand Up @@ -905,7 +897,6 @@ export class CertificateLogService {

certificatesInReservationWithLog[
index
// @ts-ignore ts(2339)
].perDeviceCertificateLog.push(singleDeviceLogEle);
});
} else {
Expand All @@ -932,7 +923,6 @@ export class CertificateLogService {
);
certificatesInReservationWithLog[
index
// @ts-ignore ts(2339)
].perDeviceCertificateLog.push(devicelog[0]);
}
}
Expand All @@ -954,7 +944,6 @@ export class CertificateLogService {
getLocalTimeZoneFromDevice(device.createdAt, device);
certificatesInReservationWithLog[
index
// @ts-ignore ts(2339)
].perDeviceCertificateLog.push(singleDeviceLogEle);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,6 @@ export class BuyerReservationController {
organizationId,
deviceGroupToRegister,
user.id,
// @ts-ignore
process.env.DREC_BLOCKCHAIN_ADDRESS,
);
}
Expand Down
Loading

0 comments on commit b20f34b

Please sign in to comment.