XPEHO / xpeapp_admin

BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Front - Admin - Update status of campaign automatically #64

Closed mmarchal closed 3 months ago

mmarchal commented 8 months ago

Create a crontab in server to change the status of campaign :

mmarchal commented 7 months ago
#!bin/bash

## Required jq (https://stedolan.github.io/jq/download/)

# Variables
STATUS_DRAFT="DRAFT"
STATUS_OPEN="OPEN"
STATUS_CLOSED="CLOSED"
STATUS_ARCHIVED="ARCHIVED"
ONE_CAMPAIGN_OPEN=false

echo "Date of execution: $(date)"

# Get the list of campaigns
campaigns=$(curl -s -X GET http://localhost:8000/wp-json/xpeho/v1/qvst/campaigns | jq -r '.[] | @base64')

# Iterate over the list of campaigns
for campaign in $campaigns
    do
        campaignId=$(echo $campaign | base64 --decode | jq -r '.id')
        campaignName=$(echo $campaign | base64 --decode | jq -r '.name')
        campaignStatus=$(echo $campaign | base64 --decode | jq -r '.status')
        campaignStartDate=$(echo $campaign | base64 --decode | jq -r '.start_date')
        campaignEndDate=$(echo $campaign | base64 --decode | jq -r '.end_date')

        # Check if the campaign is draft
        if [ "$campaignStatus" == "$STATUS_DRAFT" ]
            then
                echo "The campaign $campaignName is DRAFT with dates $campaignStartDate - $campaignEndDate"
                # Check if the campaign start date is today
                if [ "$campaignStartDate" == "$(date +%Y-%m-%d)" ]
                    then
                        # Update the campaign status to OPEN
                        curl -s -X POST -H "Content-Type: application/json" -d "{\"status\":\"$STATUS_OPEN\"}" http://localhost:8000/wp-json/xpeho/v1/qvst/campaigns/$campaignId/status:update
                        echo "The campaign $campaignName has been OPEN"
                        ONE_CAMPAIGN_OPEN=true
                fi
        fi

        # Check if the campaign is open
        if [ "$campaignStatus" == "$STATUS_OPEN" ]
            then
                echo "The campaign $campaignName is OPEN with dates $campaignStartDate - $campaignEndDate"
                # Check if the campaign end date is today
                if [ "$campaignEndDate" == "$(date +%Y-%m-%d)" ]
                    then
                        # Update the campaign status to CLOSED
                        curl -s -X POST -H "Content-Type: application/json" -d "{\"status\":\"$STATUS_CLOSED\"}" http://localhost:8000/wp-json/xpeho/v1/qvst/campaigns/$campaignId/status:update
                        echo "The campaign $campaignName has been CLOSED"
                        ONE_CAMPAIGN_OPEN=false
                fi
        fi

    done

# Connect to the firebase database
FIREBASE_EMAIL="***"
FIREBASE_PASSWORD="***"
FIREBASE_API_KEY="***"
FIREBASE_ACCESS_TOKEN=$(curl -s -X POST -H "Content-Type: application/json" -d "{\"email\":\"$FIREBASE_EMAIL\",\"password\":\"$FIREBASE_PASSWORD\",\"returnSecureToken\":true}" https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=$FIREBASE_API_KEY | jq -r '.idToken')

# Update the value enabled on the document campaign in the cloud firestore
curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Bearer $FIREBASE_ACCESS_TOKEN" -d "{\"fields\":{\"enabled\":{\"booleanValue\":\"$ONE_CAMPAIGN_OPEN\"}}}" "https://firestore.googleapis.com/v1/projects/***/databases/(default)/documents/***/***?updateMask.fieldPaths=enabled&key=$FIREBASE_API_KEY"