Skip to content

Commit 360039f

Browse files
authored
[SELC-5270] feat: Added script migration onboarding to Pec Notification (#17)
1 parent 3ebd379 commit 360039f

File tree

5 files changed

+527
-0
lines changed

5 files changed

+527
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
MONGO_HOST=mongodb://localhost:27017
2+
EPOCH_DATE_PEC_NOTIFICATION=2024-01-01
3+
SENDING_FREQUENCY_PEC_NOTIFICATION=30
4+
MIGRATE_PEC_PRODUCT_ID=prod-io,prod-pagopa,prod-pn,prod-interop,prod-io-premium,prod-io-sign
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Institution migration scripts
2+
3+
Python scripts to manipulate data about Selfcare Institution domain.
4+
5+
## Requirements
6+
7+
- Python 3.6 or higher
8+
- MongoDB
9+
- Required Python packages: `python-dotenv`, `pymongo`, `python-dateutil`
10+
11+
## Setup
12+
13+
1. **Clone the repository:**
14+
```sh
15+
git clone <repository_url>
16+
cd <repository_directory>/selfcare-institution/apps/institution-migration
17+
```
18+
19+
## institutiton_to_pec_notification
20+
21+
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:
22+
23+
1. Connects to the MongoDB database.
24+
2. Calculates the module day of the epoch for notifications.
25+
3. Retrieves institutions with active onboarding in batches.
26+
4. Creates or updates `PecNotification` documents for each institution and onboarding.
27+
28+
Create a .env file in the root directory of the project and add the environment variables inside .env.example
29+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import math
2+
import os
3+
from dotenv import load_dotenv
4+
import time
5+
from pymongo import MongoClient
6+
from datetime import datetime
7+
from dateutil.parser import parse
8+
from query import *
9+
10+
load_dotenv(dotenv_path=".env", override=True)
11+
HOST = os.getenv('MONGO_HOST')
12+
13+
CORE_DB = 'selcMsCore'
14+
INSTITUTION_COLLECTION = 'Institution'
15+
USERS_DB = 'selcMsCore'
16+
PEC_NOTIFICATION_COLLECTION = 'PecNotification'
17+
18+
epochDatePecNotification = os.getenv('EPOCH_DATE_PEC_NOTIFICATION')
19+
sendingFrequencyPecNotification = int(os.getenv('SENDING_FREQUENCY_PEC_NOTIFICATION'))
20+
#ex "productId1,productId2,productId3"
21+
productIdsString = os.getenv('MIGRATE_PEC_PRODUCT_ID')
22+
productIds = productIdsString.split(",")
23+
24+
BATCH_SIZE = 100
25+
START_PAGE = 0
26+
def institution_to_pec_notification(client):
27+
print("Starting process to create PecNotification")
28+
print("Products=" + productIdsString)
29+
30+
module_day_of_the_epoch = calculate_module_day_of_the_epoch(
31+
epochDatePecNotification, datetime.now().isoformat(), sendingFrequencyPecNotification)
32+
print("Module day of the epoch: " + str(module_day_of_the_epoch))
33+
34+
institutions_size_cursor = client[CORE_DB][INSTITUTION_COLLECTION].aggregate(count_institutions_with_active_onboarding(productIds))
35+
institutions_size = next(institutions_size_cursor)['count']
36+
print("Institutions size: " + str(institutions_size))
37+
pages = math.ceil(institutions_size / BATCH_SIZE)
38+
39+
for page in range(START_PAGE, pages):
40+
print("Start page " + str(page + 1) + "/" + str(pages))
41+
42+
institutions_pages = client[CORE_DB][INSTITUTION_COLLECTION].aggregate(
43+
get_institutions_with_active_onboarding(productIds, page, BATCH_SIZE)
44+
)
45+
46+
for institution in institutions_pages:
47+
for onboarding in institution.get('onboarding', []):
48+
if onboarding.get('status') == 'ACTIVE' and onboarding["productId"] in productIds:
49+
pec_notification_document = {
50+
"institutionId": institution["_id"],
51+
"productId": onboarding["productId"],
52+
"moduleDayOfTheEpoch": calculate_module_day_of_the_epoch(epochDatePecNotification, onboarding.get('createdAt'), sendingFrequencyPecNotification),
53+
"digitalAddress": institution.get("digitalAddress")
54+
}
55+
client[USERS_DB][PEC_NOTIFICATION_COLLECTION].update_one(
56+
{"institutionId": institution["_id"], "productId": onboarding["productId"]},
57+
{"$set": pec_notification_document, "$setOnInsert": {"createdAt": datetime.now()}},
58+
upsert=True
59+
)
60+
print("Create PecNotification for institution " + institution["_id"] + " and product " + onboarding["productId"])
61+
print("End page " + str(page + 1) + "/" + str(pages))
62+
time.sleep(15)
63+
64+
print("Completed")
65+
66+
def calculate_module_day_of_the_epoch(epoch_start_str, current_date_str, sending_frequency):
67+
epoch_start = parse(epoch_start_str).date()
68+
current_date = parse(current_date_str).date()
69+
return (current_date - epoch_start).days % sending_frequency
70+
71+
if __name__ == "__main__":
72+
client = MongoClient(HOST)
73+
institution_to_pec_notification(client)
74+
client.close()

0 commit comments

Comments
 (0)