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 2 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
121 changes: 121 additions & 0 deletions bin/delete-outdated-sendLinks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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;
};

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,
},
];

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;
};

async function start() {
const BUCKET = 'GET BUCKET';
const outdatedLinksIds = await db.query('SELECT DISTINCT(id) AS id FROM send_links WHERE expiration_at<=NOW()',
{ type: QueryTypes.SELECT }) as unknown as string[];
const outdatedSendLinkItems = await db.query(`
SELECT network_id AS "file_id", -1 AS "user_id", -1 AS "folder_id", ${BUCKET} AS "bucket", id
FROM send_links_items
WHERE type='file' AND link_id IN (${outdatedLinksIds.join('\',\'')})`, { type: QueryTypes.SELECT });
larry-internxt marked this conversation as resolved.
Show resolved Hide resolved

outdatedSendLinkItems.forEach((sendLinkitem) => {
db.query(`INSERT INTO deleted_files (file_id, user_id, folder_id, bucket) VALUES (
larry-internxt marked this conversation as resolved.
Show resolved Hide resolved
'${(sendLinkitem as DeleteSendLinkItem).file_id}', ${(sendLinkitem as DeleteSendLinkItem).user_id},
${(sendLinkitem as DeleteSendLinkItem).folder_id}, '${(sendLinkitem as DeleteSendLinkItem).bucket}')`,
larry-internxt marked this conversation as resolved.
Show resolved Hide resolved
{ type: QueryTypes.INSERT });
db.query(`DELETE FROM send_links_items WHERE id=${(sendLinkitem as DeleteSendLinkItem).id}`,
{ type: QueryTypes.DELETE });
});

outdatedLinksIds.forEach((outdatedLinkId) => {
db.query(`DELETE FROM send_links_items WHERE link_id=${outdatedLinkId}`, { type: QueryTypes.DELETE });
db.query(`DELETE FROM send_links WHERE id=${outdatedLinkId}`, { type: QueryTypes.DELETE });
});
}

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