npm package: @peyushx/campaign
Description: A campaign module for enterprise
Author: Piyush Kumar Singh
Latest version: 1.0.15

# @peyushx/campaign

The `@peyushx/campaign` library provides a campaign management module for enterprise applications. It includes support for encrypted data handling using AES encryption, allowing secure transmission and storage of sensitive campaign information.

## Features

- Campaign Management: Core services and components for managing campaigns.
- AES Encryption: Securely encrypt and decrypt data using AES, with customizable encryption keys.
- Interceptor Support: Automatically decrypt responses for specified URLs.

## Installation

To install this library, you need to install the required peer dependencies: `moment` and `crypto-js`.

Run the following command:

```sh
npm install @peyushx/campaign moment crypto-js
```

## Usage

### 1. Import the CampaignModule

To use the `CampaignModule` in your application, import it into your app’s module.

```typescript
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CampaignModule } from '@peyushx/campaign';  // Import the CampaignModule
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, CampaignModule],  // Add CampaignModule here
  bootstrap: [AppComponent]
})
export class AppModule {}
```

### 2. Configure EncryptionService

The `EncryptionService` requires an encryption key that you can provide via dependency injection. This allows flexible configuration for different environments.

Example (`app.module.ts`):

```typescript
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CampaignModule, EncryptionConfig } from '@peyushx/campaign';

@NgModule({
  imports: [BrowserModule, CampaignModule],
  providers: [
    {
      provide: 'ENCRYPTION_CONFIG',
      useValue: { secretKey: 'yourSecretKey' } as EncryptionConfig  // Provide your encryption key
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}
```

### 3. Use EncryptionService in Your Components/Services

You can inject the `EncryptionService` and use its `encryptData` and `decryptData` methods.

```typescript
import { Component } from '@angular/core';
import { EncryptionService } from '@peyushx/campaign';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html'
})
export class ExampleComponent {
  constructor(private encryptionService: EncryptionService) {}

  encryptExample() {
    const data = { message: 'Hello, World!' };
    const encryptedData = this.encryptionService.encryptData(data);
    console.log('Encrypted Data:', encryptedData);
  }

  decryptExample(encryptedData: string) {
    const decryptedData = this.encryptionService.decryptData(encryptedData);
    console.log('Decrypted Data:', decryptedData);
  }
}
```

### 4. Interceptor for Encrypted Responses

The module includes an HTTP interceptor (`AuthInterceptor`) that automatically decrypts responses from specific URLs. You can configure it by adding it to your app’s providers.

Example:

```typescript
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { AuthInterceptor } from '@peyushx/campaign';

@NgModule({
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
  ]
})
export class AppModule {}
```

The interceptor will automatically decrypt responses from the configured URLs (`http://localhost:8080` and `https://evolix-core.moglilabs.com` by default).

## API Documentation

### EncryptionService

- `encryptData(data: Object): string`
  - Encrypts the given object using AES.
  - **Parameters**: `data` (Object) – the data to be encrypted.
  - **Returns**: AES encrypted string.

- `decryptData(data: string): any`
  - Decrypts the given AES encrypted string.
  - **Parameters**: `data` (string) – the encrypted data to decrypt.
  - **Returns**: Decrypted data.

### Configuration Options

- `EncryptionConfig`
  - `secretKey`: string – The secret key used for AES encryption/decryption. You must provide this in your module configuration.

## Peer Dependencies

Ensure that the following peer dependencies are installed in your project:

- `moment`: ^2.29.1
- `crypto-js`: ^4.0.0

You can install them along with the library:

```sh
npm install @peyushx/campaign moment crypto-js
```

## Development

To contribute or modify the module:

1. Clone the repository.
2. Run `npm install` to install the dependencies.
3. Run `ng build` to build the library.
4. To test the library locally, run `npm link` and use it in another Angular project.

## License

This project is licensed under the MIT License. See the LICENSE file for more details.

## Updates in package.json

Make sure that your `package.json` reflects `moment` and `crypto-js` as peer dependencies:

```json
{
  "peerDependencies": {
    "@angular/common": "^14.2.0",
    "@angular/core": "^14.2.0",
    "moment": "^2.29.1",
    "crypto-js": "^4.0.0"
  },
  "dependencies": {
    "tslib": "^2.3.0"
  }
}
```

Feel free to open issues or submit pull requests on GitHub if you would like to contribute to this module.