Skip to content

Commit

Permalink
[SELC-5270] feat: Added script migration onboarding to Pec Notificati…
Browse files Browse the repository at this point in the history
…on (#17)
  • Loading branch information
manuraf authored Jul 25, 2024
1 parent 3ebd379 commit 360039f
Show file tree
Hide file tree
Showing 5 changed files with 527 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apps/institution-migration/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MONGO_HOST=mongodb://localhost:27017
EPOCH_DATE_PEC_NOTIFICATION=2024-01-01
SENDING_FREQUENCY_PEC_NOTIFICATION=30
MIGRATE_PEC_PRODUCT_ID=prod-io,prod-pagopa,prod-pn,prod-interop,prod-io-premium,prod-io-sign
29 changes: 29 additions & 0 deletions apps/institution-migration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Institution migration scripts

Python scripts to manipulate data about Selfcare Institution domain.

## Requirements

- Python 3.6 or higher
- MongoDB
- Required Python packages: `python-dotenv`, `pymongo`, `python-dateutil`

## Setup

1. **Clone the repository:**
```sh
git clone <repository_url>
cd <repository_directory>/selfcare-institution/apps/institution-migration
```

## institutiton_to_pec_notification

This Python script creates `PecNotification` documents for institutions with active onboarding, using data stored in a MongoDB database. The process performs the following main operations:

1. Connects to the MongoDB database.
2. Calculates the module day of the epoch for notifications.
3. Retrieves institutions with active onboarding in batches.
4. Creates or updates `PecNotification` documents for each institution and onboarding.

Create a .env file in the root directory of the project and add the environment variables inside .env.example

74 changes: 74 additions & 0 deletions apps/institution-migration/institution_to_pec_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import math
import os
from dotenv import load_dotenv
import time
from pymongo import MongoClient
from datetime import datetime
from dateutil.parser import parse
from query import *

load_dotenv(dotenv_path=".env", override=True)
HOST = os.getenv('MONGO_HOST')

CORE_DB = 'selcMsCore'
INSTITUTION_COLLECTION = 'Institution'
USERS_DB = 'selcMsCore'
PEC_NOTIFICATION_COLLECTION = 'PecNotification'

epochDatePecNotification = os.getenv('EPOCH_DATE_PEC_NOTIFICATION')
sendingFrequencyPecNotification = int(os.getenv('SENDING_FREQUENCY_PEC_NOTIFICATION'))
#ex "productId1,productId2,productId3"
productIdsString = os.getenv('MIGRATE_PEC_PRODUCT_ID')
productIds = productIdsString.split(",")

BATCH_SIZE = 100
START_PAGE = 0
def institution_to_pec_notification(client):
print("Starting process to create PecNotification")
print("Products=" + productIdsString)

module_day_of_the_epoch = calculate_module_day_of_the_epoch(
epochDatePecNotification, datetime.now().isoformat(), sendingFrequencyPecNotification)
print("Module day of the epoch: " + str(module_day_of_the_epoch))

institutions_size_cursor = client[CORE_DB][INSTITUTION_COLLECTION].aggregate(count_institutions_with_active_onboarding(productIds))
institutions_size = next(institutions_size_cursor)['count']
print("Institutions size: " + str(institutions_size))
pages = math.ceil(institutions_size / BATCH_SIZE)

for page in range(START_PAGE, pages):
print("Start page " + str(page + 1) + "/" + str(pages))

institutions_pages = client[CORE_DB][INSTITUTION_COLLECTION].aggregate(
get_institutions_with_active_onboarding(productIds, page, BATCH_SIZE)
)

for institution in institutions_pages:
for onboarding in institution.get('onboarding', []):
if onboarding.get('status') == 'ACTIVE' and onboarding["productId"] in productIds:
pec_notification_document = {
"institutionId": institution["_id"],
"productId": onboarding["productId"],
"moduleDayOfTheEpoch": calculate_module_day_of_the_epoch(epochDatePecNotification, onboarding.get('createdAt'), sendingFrequencyPecNotification),
"digitalAddress": institution.get("digitalAddress")
}
client[USERS_DB][PEC_NOTIFICATION_COLLECTION].update_one(
{"institutionId": institution["_id"], "productId": onboarding["productId"]},
{"$set": pec_notification_document, "$setOnInsert": {"createdAt": datetime.now()}},
upsert=True
)
print("Create PecNotification for institution " + institution["_id"] + " and product " + onboarding["productId"])
print("End page " + str(page + 1) + "/" + str(pages))
time.sleep(15)

print("Completed")

def calculate_module_day_of_the_epoch(epoch_start_str, current_date_str, sending_frequency):
epoch_start = parse(epoch_start_str).date()
current_date = parse(current_date_str).date()
return (current_date - epoch_start).days % sending_frequency

if __name__ == "__main__":
client = MongoClient(HOST)
institution_to_pec_notification(client)
client.close()
Loading

0 comments on commit 360039f

Please sign in to comment.