|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const AWS = require('aws-sdk'); |
| 4 | + |
| 5 | +const OBJ_STORE_URL = process.env.OBJECT_STORE_endpoint_url || 'nrs.objectstore.gov.bc.ca'; |
| 6 | +const ep = new AWS.Endpoint(OBJ_STORE_URL); |
| 7 | +const s3 = new AWS.S3({ |
| 8 | + endpoint: ep, |
| 9 | + accessKeyId: process.env.OBJECT_STORE_user_account, |
| 10 | + secretAccessKey: process.env.OBJECT_STORE_password, |
| 11 | + signatureVersion: 'v4', |
| 12 | + s3ForcePathStyle: true |
| 13 | +}); |
| 14 | + |
| 15 | +/** |
| 16 | + * We receive the dbmigrate dependency from dbmigrate initially. |
| 17 | + * This enables us to not have to rely on NODE_PATH. |
| 18 | + */ |
| 19 | +exports.setup = function(options, seedLink) {}; |
| 20 | + |
| 21 | +exports.up = async function(db) { |
| 22 | + const mClient = await db.connection.connect(db.connectionString, { |
| 23 | + native_parser: true |
| 24 | + }); |
| 25 | + |
| 26 | + try { |
| 27 | + console.log(`Removing G-4-352 permits`); |
| 28 | + |
| 29 | + // Only need nrpti collection because these records aren't published |
| 30 | + const nrpti = await mClient.collection('nrpti'); |
| 31 | + |
| 32 | + let dbRecordsToDelete = []; |
| 33 | + |
| 34 | + const records = await nrpti |
| 35 | + .find({ mineGuid: '6bac3584-ab40-41fb-992a-31cc1e92a9ae', permitNumber: 'G-4-352' }) |
| 36 | + .toArray(); |
| 37 | + |
| 38 | + dbRecordsToDelete = records.map(record => record._id); |
| 39 | + |
| 40 | + const documents = await nrpti |
| 41 | + .find({ |
| 42 | + _id: { |
| 43 | + // Flatten record documents array |
| 44 | + $in: [].concat.apply( |
| 45 | + [], |
| 46 | + records.map(record => record.documents) |
| 47 | + ) |
| 48 | + } |
| 49 | + }) |
| 50 | + .toArray(); |
| 51 | + |
| 52 | + dbRecordsToDelete = dbRecordsToDelete.concat(documents.map(document => document._id)); |
| 53 | + |
| 54 | + await deleteS3Documents( |
| 55 | + documents.map(document => { |
| 56 | + return { |
| 57 | + Key: document.key |
| 58 | + }; |
| 59 | + }) |
| 60 | + ); |
| 61 | + |
| 62 | + await nrpti.deleteMany({ _id: { $in: dbRecordsToDelete } }); |
| 63 | + |
| 64 | + console.log(`Finished removing G-4-352 permits`); |
| 65 | + } catch (err) { |
| 66 | + console.log(`Error removing G-4-352 permits: ${err}`); |
| 67 | + } finally { |
| 68 | + mClient.close(); |
| 69 | + } |
| 70 | + |
| 71 | + return null; |
| 72 | +}; |
| 73 | + |
| 74 | +async function deleteS3Documents(s3Keys) { |
| 75 | + if (!process.env.OBJECT_STORE_bucket_name) { |
| 76 | + throw new Error('Missing required OBJECT_STORE_bucket_name env variable'); |
| 77 | + } |
| 78 | + |
| 79 | + if (!s3Keys) { |
| 80 | + throw new Error('Missing required s3Key param'); |
| 81 | + } |
| 82 | + |
| 83 | + let params = { |
| 84 | + Bucket: process.env.OBJECT_STORE_bucket_name, |
| 85 | + Delete: { |
| 86 | + Objects: s3Keys |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + return s3.deleteObjects(params).promise(); |
| 91 | +} |
| 92 | + |
| 93 | +exports.down = function(db) { |
| 94 | + return null; |
| 95 | +}; |
| 96 | + |
| 97 | +exports._meta = { |
| 98 | + version: 1 |
| 99 | +}; |
0 commit comments