-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c003cb
commit 42c44b8
Showing
9 changed files
with
594 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
migrations/1671010308952_create-agegroup-completed-table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
exports.shorthands = undefined | ||
|
||
exports.up = (pgm) => { | ||
pgm.createType('agegroup_state', ['COMPLETED']) | ||
pgm.createTable('completed_agegroup_entries', { | ||
id: 'id', | ||
user_guid: { | ||
type: 'text', | ||
notNull: true, | ||
}, | ||
created_at: { | ||
type: 'timestamp', | ||
default: pgm.func('NOW()'), | ||
}, | ||
created_by: { | ||
type: 'text', | ||
notNull: true, | ||
}, | ||
agegroup_guid: { | ||
type: 'text', | ||
notNull: true, | ||
}, | ||
completion_status: { | ||
type: 'agegroup_state', | ||
notNull: true, | ||
}, | ||
}) | ||
} | ||
|
||
exports.down = async (pgm) => { | ||
// Make sure we don't have any data we can lose in the table when doing a migrate down | ||
const hasRows = | ||
(await pgm.db.query('SELECT id FROM completed_agegroup_entries')).rows | ||
.length > 0 | ||
if (hasRows) { | ||
throw new Error(` | ||
Migrate down aborted - possible lose of data if completed_agegroup_entries table is dropped: \n | ||
Make sure you've emptied the table before dropping. | ||
`) | ||
} | ||
pgm.dropTable('completed_agegroup_entries') | ||
pgm.dropType('agegroup_state') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { db } from './index' | ||
import { createNotification } from './notifications' | ||
|
||
export async function postAgegroupEntry(ageGroupEntry) { | ||
const { user_guid, created_by, agegroup_guid, completed, group_leader_name } = | ||
ageGroupEntry | ||
|
||
try { | ||
// Create an entry for the agegroup entry state change | ||
const data = await db.one( | ||
'INSERT INTO completed_agegroup_entries(user_guid, created_by, agegroup_guid, completion_status) VALUES ($1, $2, $3, $4) RETURNING id', | ||
[user_guid, created_by, agegroup_guid, completed] | ||
) | ||
|
||
const entry = await db.one( | ||
'SELECT agegroup_guid, completion_status FROM completed_agegroup_entries WHERE id = $1', | ||
data.id | ||
) | ||
|
||
// Create a notification about the state change | ||
const notification = await createNotification({ | ||
itemGuid: agegroup_guid, | ||
itemType: 'AGE_GROUP', | ||
notificationType: completed, | ||
userGuid: user_guid, | ||
createdBy: created_by, | ||
groupLeaderName: group_leader_name, | ||
}) | ||
if (!notification) { | ||
throw new Error('Failed to create a notification.') | ||
} | ||
|
||
return entry | ||
} catch (error) { | ||
console.error('post agegroup entry - error: ', error) | ||
} | ||
} | ||
|
||
export async function getAgeGroupEntries(user_guid) { | ||
try { | ||
const data = await db.any( | ||
'SELECT agegroup_guid, completion_status FROM completed_agegroup_entries WHERE user_guid = $1 ORDER BY created_at ASC', | ||
user_guid.toString() | ||
) | ||
return data | ||
} catch (error) { | ||
console.log('error', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
require('dotenv').config() | ||
const pgp = require('pg-promise')() | ||
|
||
const db = pgp(process.env.DATABASE_URL) | ||
const unusedActivities = require('./get-unused-activities') | ||
|
||
async function deleteTaskEntriesById(task_guid) { | ||
try { | ||
const data = await db.any( | ||
'DELETE FROM task_entries WHERE task_guid = $1 RETURNING task_guid', | ||
[task_guid.toString()] | ||
) | ||
|
||
console.log('entries successfully deleted', data) | ||
return data | ||
} catch (error) { | ||
console.log('delete taskentries - error', error) | ||
} | ||
} | ||
|
||
async function main() { | ||
const oldIds = unusedActivities.main() | ||
const resolvedIds = await oldIds | ||
resolvedIds.forEach((task_guid) => { | ||
try { | ||
return deleteTaskEntriesById(task_guid) | ||
} catch (error) { | ||
console.log('error while migrating', error) | ||
} | ||
}) | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
require('dotenv').config() | ||
const fs = require('fs') | ||
var path = require('path') | ||
var request = require('request-promise') | ||
|
||
const DBURL = process.env.POF_BACKEND_STAGING | ||
async function fetchActivitiesFromStrapi() { | ||
try { | ||
const countRes = await request(`${DBURL}/activities/count?_locale=fi`) | ||
const activities = await request(`${DBURL}/activities?_limit=${countRes}`) | ||
|
||
return activities | ||
} catch (e) { | ||
console.log(`Error getting activities: ${e}`) | ||
return null | ||
} | ||
} | ||
|
||
const sortArraysAscending = (array) => { | ||
return array.sort(function (a, b) { | ||
return a - b | ||
}) | ||
} | ||
|
||
const uniqueValues = (value, index, self) => { | ||
return self.indexOf(value) === index | ||
} | ||
|
||
function writeUnusedActivitiesToTxtFile(ids) { | ||
const writeStream = fs.createWriteStream('unusedActivityIds.txt') | ||
const pathName = writeStream.path | ||
|
||
// write each value of the array on the file breaking line | ||
ids.forEach((value) => writeStream.write(`${value}\n`)) | ||
|
||
writeStream.on('finish', () => { | ||
console.log(`wrote all the array data to file ${pathName}`) | ||
}) | ||
writeStream.on('error', (err) => { | ||
console.error(`There is an error writing the file ${pathName} => ${err}`) | ||
}) | ||
writeStream.end() | ||
} | ||
|
||
async function main() { | ||
// Change filename below to match the csv-file with migration activities | ||
const filePath = path.join(__dirname, './aktiviteetti_aa.csv') | ||
// Read CSV | ||
let file = fs.readFileSync(filePath, { encoding: 'utf-8' }, function (err) { | ||
console.log(err) | ||
}) | ||
|
||
// Split on row | ||
file = file.split('\n') | ||
|
||
// Get first row for column headers | ||
headers = file.shift().split(',') | ||
|
||
let json = [] | ||
file.forEach(function (row) { | ||
// Loop through each row | ||
tmp = {} | ||
row = row.split(',') | ||
for (let i = 0; i < headers.length; i++) { | ||
tmp[headers[i]] = row[i] | ||
} | ||
// Add object to list | ||
json.push(tmp) | ||
}) | ||
|
||
const activityIdsFromKuksa = json.map((row) => { | ||
return row.activities_Partioaktiviteetti_Yhdistä1_aktiviteetti_View_id | ||
}) | ||
|
||
const uniqueIdValuesInOrder = sortArraysAscending( | ||
activityIdsFromKuksa.filter(uniqueValues) | ||
) | ||
|
||
const activityidsFromStrapiPromise = fetchActivitiesFromStrapi().then( | ||
function (activities) { | ||
const activitiesJson = JSON.parse(activities) | ||
const ids = activitiesJson.map((activity) => { | ||
return activity.id.toString() | ||
}) | ||
return sortArraysAscending(ids) | ||
} | ||
) | ||
|
||
const activityIdsFromStrapi = await Promise.resolve( | ||
activityidsFromStrapiPromise | ||
) | ||
|
||
const oldIdsFromKuksa = uniqueIdValuesInOrder.filter( | ||
(x) => !activityIdsFromStrapi.includes(x) | ||
) | ||
|
||
if (oldIdsFromKuksa.length) { | ||
writeUnusedActivitiesToTxtFile(oldIdsFromKuksa) | ||
return oldIdsFromKuksa | ||
} else { | ||
console.log('No old ids') | ||
} | ||
} | ||
|
||
main() | ||
module.exports = { main } |
Oops, something went wrong.