Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WT-161]: Feature/Move outdated sendLinks script #330

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions bin/delete-outdated-sendLinks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { QueryTypes } from 'sequelize';
import { Command } from 'commander';

import Database from '../../src/config/initializers/database';

type CommandOptions = {
dbHostname: string;
dbPort: string;
dbName: string;
dbUsername: string;
dbPassword: string;
bucket: string;
};

const commands: { flags: string; description: string; required: boolean }[] = [
{
flags: '--db-hostname <database_hostname>',
description: 'The hostname of the database where deleted files are stored',
required: true,
},
{
flags: '--db-name <database_name>',
description: 'The name of the database where deleted files are stored',
required: true,
},
{
flags: '--db-username <database_username>',
description: 'The username authorized to read and delete from the deleted files table',
required: true,
},
{
flags: '--db-password <database_password>',
description: 'The database username password',
required: true,
},
{
flags: '--db-port <database_port>',
description: 'The database port',
required: true,
},
{
flags: '-b, --bucket <bucket>',
description: 'The bucket ID currently used on SEND',
required: true,
},
];

const command = new Command('delete-outdated-sendLinks').version('0.0.1');

commands.forEach((c) => {
if (c.required) {
command.requiredOption(c.flags, c.description);
} else {
command.option(c.flags, c.description);
}
});

command.parse();

const opts: CommandOptions = command.opts();
const db = Database.getInstance({
sequelizeConfig: {
host: opts.dbHostname,
port: opts.dbPort,
database: opts.dbName,
username: opts.dbUsername,
password: opts.dbPassword,
dialect: 'postgres',
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
},
});

function finishProgram() {
db.close()
.then(() => {
console.log('DISCONNECTED FROM DB');
})
.catch((err) => {
console.log('Error closing connection %s. %s', err.message.err.stack || 'NO STACK.');
});
}
process.on('SIGINT', () => finishProgram());

interface DeleteSendLinkItem {
file_id: string;
user_id: number;
folder_id: number;
bucket: string;
id: string;
link_id: string;
};

async function start(bucket: string) {
const outdatedLinksIds = [] as string[];
const outdatedSendLinkItems = await db.query(`
SELECT sli.network_id AS "file_id", -1 AS "user_id", -1 AS "folder_id", :bucket AS "bucket",
sli.id as id, sli.link_id as link_id
FROM send_links_items sli
INNER JOIN send_links sl ON sl.id=sli.link_id
WHERE sl.expiration_at<=NOW() AND sli.type='file'`,
{
type: QueryTypes.SELECT,
replacements: {
bucket: bucket
}
});

for (const sendLinkItem of outdatedSendLinkItems as DeleteSendLinkItem[]) {
if (!outdatedLinksIds.includes(sendLinkItem.link_id)) {
outdatedLinksIds.push(sendLinkItem.link_id);
}

await db.query(`INSERT INTO deleted_files (file_id, user_id, folder_id, bucket)
VALUES (:file_id, :user_id, :folder_id, :bucket)`,
{
type: QueryTypes.INSERT,
replacements: {
file_id: sendLinkItem.file_id,
user_id: sendLinkItem.user_id,
folder_id: sendLinkItem.folder_id,
bucket: sendLinkItem.bucket,
}
});
await db.query('DELETE FROM send_links_items WHERE id=:id',
{
type: QueryTypes.DELETE,
replacements: {
id: sendLinkItem.id
}
});
}

for (const outdatedLinkId of outdatedLinksIds) {
await db.query('DELETE FROM send_links_items WHERE link_id=:link_id',
{
type: QueryTypes.DELETE,
replacements: {
link_id: outdatedLinkId
}
});
await db.query('DELETE FROM send_links WHERE id=:id',
{
type: QueryTypes.DELETE,
replacements: {
id: outdatedLinkId
}
});
}
}

start(String(opts.bucket))
.catch((err) => {
console.log('err', err);
})
.finally(() => {
finishProgram();
});