Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ORV2-2223 Policy Config Api - Phase 2 #1488

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ SET IDENTITY_INSERT [dbo].[ORBC_FEATURE_FLAG] ON
,[DB_LAST_UPDATE_TIMESTAMP])
VALUES ('2','APPLICATION-SEARCH','ENABLED', NULL, N'dbo', GETUTCDATE(), N'dbo', GETUTCDATE());

INSERT INTO [dbo].[ORBC_FEATURE_FLAG] ([FEATURE_ID]
,[FEATURE_KEY]
,[FEATURE_VALUE]
,[CONCURRENCY_CONTROL_NUMBER]
,[DB_CREATE_USERID]
,[DB_CREATE_TIMESTAMP]
,[DB_LAST_UPDATE_USERID]
,[DB_LAST_UPDATE_TIMESTAMP])
VALUES ('3','POLICY-CONFIG','ENABLED', NULL, N'dbo', GETUTCDATE(), N'dbo', GETUTCDATE());

SET IDENTITY_INSERT [dbo].[ORBC_FEATURE_FLAG] OFF
GO
31 changes: 31 additions & 0 deletions database/mssql/scripts/versions/revert/v_32_ddl_revert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON
GO

SET XACT_ABORT ON

BEGIN TRY
BEGIN TRANSACTION

DROP CONSTRAINT [DF_ORBC_POLICY_CONFIGURATION_IS_DRAFT]
ALTER TABLE [dbo].[ORBC_POLICY_CONFIGURATION] DROP COLUMN [APP_LAST_UPDATE_USERID];

DELETE FROM [access].[ORBC_GROUP_ROLE] WHERE ROLE_TYPE IN ('ORBC-WRITE-POLICY-CONFIG','ORBC-READ-POLICY-CONFIG')
DELETE FROM [access].[ORBC_ROLE_TYPE] WHERE ROLE_TYPE IN ('ORBC-WRITE-POLICY-CONFIG','ORBC-READ-POLICY-CONFIG')

COMMIT
END TRY

BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK;
THROW
END CATCH

DECLARE @VersionDescription VARCHAR(255)
SET @VersionDescription = 'Reverting addition of APP_CREATE_USERID and APP_LAST_UPDATE_USERID to ORBC_POLICY_CONFIGURATION.'

INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [RELEASE_DATE]) VALUES (31, @VersionDescription, getutcdate())
58 changes: 58 additions & 0 deletions database/mssql/scripts/versions/v_32_ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET NOCOUNT ON
GO

SET XACT_ABORT ON
GO

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

BEGIN TRANSACTION
GO

IF @@ERROR <> 0
SET NOEXEC ON
GO

INSERT [access].[ORBC_ROLE_TYPE] ([ROLE_TYPE], [ROLE_DESCRIPTION]) VALUES (N'ORBC-WRITE-POLICY-CONFIG', NULL)
INSERT [access].[ORBC_ROLE_TYPE] ([ROLE_TYPE], [ROLE_DESCRIPTION]) VALUES (N'ORBC-READ-POLICY-CONFIG', NULL)
GO

INSERT [access].[ORBC_GROUP_ROLE] ([USER_AUTH_GROUP_TYPE], [ROLE_TYPE]) VALUES (N'SYSADMIN', N'ORBC-WRITE-POLICY-CONFIG')
INSERT [access].[ORBC_GROUP_ROLE] ([USER_AUTH_GROUP_TYPE], [ROLE_TYPE]) VALUES (N'SYSADMIN', N'ORBC-READ-POLICY-CONFIG')

ALTER TABLE [dbo].[ORBC_POLICY_CONFIGURATION] ADD [APP_LAST_UPDATE_USERID] [nvarchar](30) NULL
GO

ALTER TABLE [dbo].[ORBC_POLICY_CONFIGURATION] ADD CONSTRAINT [DF_ORBC_POLICY_CONFIGURATION_IS_DRAFT] DEFAULT ('Y') FOR [IS_DRAFT]
GO

IF @@ERROR <> 0 SET NOEXEC ON
GO

DECLARE @VersionDescription VARCHAR(255)
SET @VersionDescription = 'Add APP_LAST_UPDATE_USERID cols to ORBC_POLICY_CONFIGURATION'

INSERT [dbo].[ORBC_SYS_VERSION] ([VERSION_ID], [DESCRIPTION], [UPDATE_SCRIPT], [REVERT_SCRIPT], [RELEASE_DATE]) VALUES (32, @VersionDescription, '$(UPDATE_SCRIPT)', '$(REVERT_SCRIPT)', getutcdate())
IF @@ERROR <> 0 SET NOEXEC ON
GO

COMMIT TRANSACTION
GO
IF @@ERROR <> 0 SET NOEXEC ON
GO
DECLARE @Success AS BIT
SET @Success = 1
SET NOEXEC OFF
IF (@Success = 1) PRINT 'The database update succeeded'
ELSE BEGIN
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
PRINT 'The database update failed'
END
GO
4 changes: 4 additions & 0 deletions database/mssql/test/versions/v_32_1_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Test that the APP_LAST_UPDATE_USERID column has been added correctly
SET NOCOUNT ON

select COL_LENGTH('$(DB_NAME).[dbo].[ORBC_POLICY_CONFIGURATION]', 'APP_LAST_UPDATE_USERID')
5 changes: 5 additions & 0 deletions database/mssql/test/versions/v_32_2_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Test that the role types have been inserted correctly
SET NOCOUNT ON

SELECT COUNT(*) FROM $(DB_NAME).[access].[ORBC_ROLE_TYPE]
WHERE ROLE_TYPE IN ('ORBC-WRITE-POLICY-CONFIG','ORBC-READ-POLICY-CONFIG')
5 changes: 5 additions & 0 deletions database/mssql/test/versions/v_32_3_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Test that the role types have been inserted correctly against user auth groups
SET NOCOUNT ON

SELECT COUNT(*) FROM $(DB_NAME).[access].[ORBC_GROUP_ROLE]
WHERE ROLE_TYPE IN ('ORBC-WRITE-POLICY-CONFIG','ORBC-READ-POLICY-CONFIG')
30 changes: 30 additions & 0 deletions database/mssql/test/versions/v_32_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# Retrieve arguments
source ${SCRIPT_DIR}/utility/getopt.sh
USAGE="-u USER -p PASS -s SERVER -d DATABASE"
parse_options "${USAGE}" ${@}

# All database tests for database version 32 are run from this shell script.
# TESTS_DIR variable set by the calling test-runner script.

TEST_32_1_RESULT=$(/opt/mssql-tools/bin/sqlcmd -U ${USER} -P "${PASS}" -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${TESTS_DIR}/v_32_1_test.sql | xargs)
if [[ $TEST_32_1_RESULT -eq 30 ]]; then
echo "Test 32.1 passed: APP_LAST_UPDATE_USERID column created in ORBC_POLICY_CONFIGURATION"
else
echo "******** Test 32.1 failed: APP_LAST_UPDATE_USERID column missing in ORBC_POLICY_CONFIGURATION"
fi

TEST_32_2_RESULT=$(/opt/mssql-tools/bin/sqlcmd -U ${USER} -P "${PASS}" -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${TESTS_DIR}/v_32_2_test.sql | xargs)
if [[ $TEST_32_2_RESULT -eq 2 ]]; then
echo "Test 32.2 passed: Role types inserted correctly"
else
echo "******** Test 32.2 failed: Role types not inserted correctly"
fi

TEST_32_3_RESULT=$(/opt/mssql-tools/bin/sqlcmd -U ${USER} -P "${PASS}" -S ${SERVER} -v DB_NAME=${DATABASE} -h -1 -i ${TESTS_DIR}/v_32_3_test.sql | xargs)
if [[ $TEST_32_3_RESULT -eq 2 ]]; then
echo "Test 32.3 passed: Correct number of role mappings inserted"
else
echo "******** Test 32.3 failed: Incorrect number of role mappings inserted"
fi
4 changes: 3 additions & 1 deletion dops/src/enum/roles.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ export enum Role {
WRITE_LCV_FLAG = 'ORBC-WRITE-LCV-FLAG',
READ_LOA = 'ORBC-READ-LOA',
WRITE_LOA = 'ORBC-WRITE-LOA',
SEND_NOTIFICATION = 'ORBC-SEND-NOTIFICATION',
SEND_NOTIFICATION = 'ORBC-SEND-NOTIFICATION',
READ_POLICY_CONFIG = 'ORBC-READ-POLICY-CONFIG',
WRITE_POLICY_CONFIG = 'ORBC-WRITE-POLICY-CONFIG',
}
2 changes: 2 additions & 0 deletions policy/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { getTypeormLogLevel } from './helper/logger.helper';
import { ClsModule } from 'nestjs-cls';
import { Request } from 'express';
import { v4 as uuidv4 } from 'uuid';
import { PolicyConfigModule } from './modules/policy-config/policy-config.module';

const envPath = path.resolve(process.cwd() + '/../');

Expand Down Expand Up @@ -72,6 +73,7 @@ const envPath = path.resolve(process.cwd() + '/../');
CommonModule,
AuthModule,
FeatureFlagsModule,
PolicyConfigModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
22 changes: 22 additions & 0 deletions policy/src/decorator/is-feature-flag-enabled.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Reflector } from '@nestjs/core';

/**
* Decorator to check if a specific feature flag is enabled.
*
* The feature flag can be used to conditionally enable or disable parts of the application
* depending on the current configuration or environment setup.
*
* Usage:
*
* ```typescript
* @IsFeatureFlagEnabled('featureName')
* async someFunction() {
* // function implementation
* }
* ```
*
* @param {string} flagName - The name of the feature flag to check.
* @returns {MethodDecorator} The method decorator to be applied.
*/

export const IsFeatureFlagEnabled = Reflector.createDecorator<string>();
2 changes: 2 additions & 0 deletions policy/src/enum/roles.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ export enum Role {
READ_LOA = 'ORBC-READ-LOA',
WRITE_LOA = 'ORBC-WRITE-LOA',
SEND_NOTIFICATION = 'ORBC-SEND-NOTIFICATION',
READ_POLICY_CONFIG = 'ORBC-READ-POLICY-CONFIG',
WRITE_POLICY_CONFIG = 'ORBC-WRITE-POLICY-CONFIG',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsInt, IsPositive } from 'class-validator';
import { Type } from 'class-transformer';

export class PolicyConfigIdPathParamDto {
@ApiProperty({
example: 1,
description:
'The unique identifier of the policy configuration. This field is required.',
required: true,
})
@IsInt()
@IsPositive()
@Type(() => Number)
policyConfigId: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsOptional } from 'class-validator';
import { Transform } from 'class-transformer';

export class GetPolicyConfigQueryParamsDto {
@ApiProperty({
description:
'A flag indicating to fetch only the current policy configuration.',
example: true,
default: false,
required: false,
})
@IsOptional()
@Transform(({ obj, key }: { obj: Record<string, unknown>; key: string }) => {
return obj[key] === 'true' ? true : obj[key] === 'false' ? false : obj[key];
})
@IsBoolean()
isCurrent?: boolean = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { AutoMap } from '@automapper/classes';
import { ApiProperty } from '@nestjs/swagger';

export class ReadPolicyConfigDto {
/**
* Unique identifier for the policy configuration.
*/
@AutoMap()
@ApiProperty({
example: '1',
description: 'Unique identifier for the policy configuration.',
})
policyConfigId: number;

/**
* JSON data representing the policy configuration.
*/
@AutoMap()
@ApiProperty({
description: 'Policy configuration in JSON format.',
})
policy: JSON;

/**
* Configuration effective date.
*/
@AutoMap()
@ApiProperty({
example: '2023-07-13T17:31:17.470Z',
description: 'Policy Configuration effective date.',
})
effectiveDate: string;

/**
* Indicates if the configuration is currently a draft version.
*/
@AutoMap()
@ApiProperty({
example: true,
description: 'Indicates if the configuration is currently a draft.',
})
isDraft: boolean;

/**
* Description of changes made in the configuration.
*/
@AutoMap()
@ApiProperty({
example: 'Initial release of policy configuration with updated rules',
description: 'Description of changes made in the configuration.',
})
changeDescription: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { Base } from '../../common/entities/base.entity';
import { AutoMap } from '@automapper/classes';
import { ApiProperty } from '@nestjs/swagger';

@Entity({ name: 'ORBC_POLICY_CONFIGURATION', schema: 'dbo' })
export class PolicyConfig extends Base {
/**
* Unique identifier for the policy configuration.
*/
@AutoMap()
@ApiProperty({
example: '1',
description: 'Unique identifier for the policy configuration.',
})
@PrimaryGeneratedColumn({ type: 'int', name: 'POLICY_CONFIGURATION_ID' })
policyConfigId: number;

/**
* JSON data representing the policy configuration.
*/
@AutoMap()
@Column({ name: 'POLICY_JSON', nullable: true, type: 'simple-json' }) //Full text search capabilities is not required on the field
policy: JSON;

/**
* configuration effective date.
*/
@AutoMap()
@ApiProperty({
example: '2023-07-13T17:31:17.470Z',
description: 'Configuration effective date.',
})
@Column({
name: 'EFFECTIVE_DATE',
nullable: true,
type: 'datetime2',
})
effectiveDate: Date;

/**
* Specifies whether the config is currently active. 'Y' for yes, 'N' for no.
*/
@AutoMap()
@Column({
type: 'char',
name: 'IS_DRAFT',
default: true,
nullable: false,
transformer: {
to: (value: boolean): string => (value ? 'Y' : 'N'), // Converts the boolean value to 'Y' or 'N' for storage.
from: (value: string): boolean => value === 'Y', // Converts the stored string back to a boolean.
},
})
isDraft: boolean;

/**
* Description of changes made in the configuration.
*/
@AutoMap()
@ApiProperty({
example: 'Initial release of policy configuration with updated rules',
description: 'Description of changes made in the configuration.',
})
@Column({
name: 'CHANGE_DESCRIPTION',
nullable: true,
type: 'nvarchar',
length: 2000,
})
changeDescription: string;
}
Loading