fix(scripts): don't encrypt/decrypt missing values

This commit is contained in:
Isaac 2025-01-11 23:49:26 +00:00
parent 8fded79263
commit 87b9974dbd
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
2 changed files with 23 additions and 14 deletions

View File

@ -21,6 +21,11 @@ const file_path = join(process.cwd(), './user/dumps', `${hash}.dump`);
const file_cryptr = new Cryptr(options.guild); const file_cryptr = new Cryptr(options.guild);
const db_cryptr = new Cryptr(process.env.ENCRYPTION_KEY); const db_cryptr = new Cryptr(process.env.ENCRYPTION_KEY);
function decryptIfExists(encrypted) {
if (encrypted) return db_cryptr.decrypt(encrypted);
return null;
}
fse.ensureDirSync(join(process.cwd(), './user/dumps')); fse.ensureDirSync(join(process.cwd(), './user/dumps'));
let spinner = ora('Connecting').start(); let spinner = ora('Connecting').start();
@ -73,30 +78,30 @@ dump.tickets = await prisma.ticket.findMany({
where: { guildId: options.guild }, where: { guildId: options.guild },
}); });
dump.tickets = dump.tickets.map(ticket => { dump.tickets = dump.tickets.map(ticket => {
if (ticket.topic) ticket.topic = db_cryptr.decrypt(ticket.topic); if (ticket.topic) ticket.topic = decryptIfExists(ticket.topic);
ticket.archivedChannels = ticket.archivedChannels.map(channel => { ticket.archivedChannels = ticket.archivedChannels.map(channel => {
channel.name = db_cryptr.decrypt(channel.name); channel.name = decryptIfExists(channel.name);
return channel; return channel;
}); });
ticket.archivedMessages = ticket.archivedMessages.map(message => { ticket.archivedMessages = ticket.archivedMessages.map(message => {
message.content = db_cryptr.decrypt(message.content); message.content = decryptIfExists(message.content);
return message; return message;
}); });
ticket.archivedUsers = ticket.archivedUsers.map(user => { ticket.archivedUsers = ticket.archivedUsers.map(user => {
user.displayName = db_cryptr.decrypt(user.displayName); user.displayName = decryptIfExists(user.displayName);
user.username = db_cryptr.decrypt(user.username); user.username = decryptIfExists(user.username);
return user; return user;
}); });
if (ticket.feedback?.comment) { if (ticket.feedback?.comment) {
ticket.feedback.comment = db_cryptr.decrypt(ticket.feedback.comment); ticket.feedback.comment = decryptIfExists(ticket.feedback.comment);
} }
ticket.questionAnswers = ticket.questionAnswers.map(answer => { ticket.questionAnswers = ticket.questionAnswers.map(answer => {
if (answer.value) answer.value = db_cryptr.decrypt(answer.value); if (answer.value) answer.value = decryptIfExists(answer.value);
return answer; return answer;
}); });

View File

@ -21,6 +21,10 @@ const hash = createHash('sha256').update(options.guild).digest('hex');
const file_cryptr = new Cryptr(options.guild); const file_cryptr = new Cryptr(options.guild);
const db_cryptr = new Cryptr(process.env.ENCRYPTION_KEY); const db_cryptr = new Cryptr(process.env.ENCRYPTION_KEY);
function encryptIfExists(plain_text) {
if (plain_text) return db_cryptr.encrypt(plain_text);
return null;
}
let spinner = ora('Connecting').start(); let spinner = ora('Connecting').start();
@ -98,12 +102,12 @@ for (const i in dump.tickets) {
const ticket = dump.tickets[i]; const ticket = dump.tickets[i];
ticket.category = { connect: { id: category_map[ticket.categoryId] } }; ticket.category = { connect: { id: category_map[ticket.categoryId] } };
if (ticket.topic) ticket.topic = db_cryptr.encrypt(ticket.topic); if (ticket.topic) ticket.topic = encryptIfExists(ticket.topic);
ticket.archivedChannels = { ticket.archivedChannels = {
create: ticket.archivedChannels.map(channel => { create: ticket.archivedChannels.map(channel => {
delete channel.ticketId; delete channel.ticketId;
channel.name = db_cryptr.encrypt(channel.name); channel.name = encryptIfExists(channel.name);
return channel; return channel;
}), }),
}; };
@ -111,8 +115,8 @@ for (const i in dump.tickets) {
ticket.archivedUsers = { ticket.archivedUsers = {
create: ticket.archivedUsers.map(user => { create: ticket.archivedUsers.map(user => {
delete user.ticketId; delete user.ticketId;
user.displayName = db_cryptr.encrypt(user.displayName); user.displayName = encryptIfExists(user.displayName);
user.username = db_cryptr.encrypt(user.username); user.username = encryptIfExists(user.username);
return user; return user;
}), }),
}; };
@ -125,7 +129,7 @@ for (const i in dump.tickets) {
}; };
const archivedMessages = ticket.archivedMessages.map(message => { const archivedMessages = ticket.archivedMessages.map(message => {
message.content = db_cryptr.encrypt(message.content); message.content = encryptIfExists(message.content);
return message; return message;
}); });
ticket.archivedMessages = undefined; ticket.archivedMessages = undefined;
@ -135,7 +139,7 @@ for (const i in dump.tickets) {
delete ticket.feedback.guildId; delete ticket.feedback.guildId;
ticket.feedback.guild = { connect: { id: options.guild } }; ticket.feedback.guild = { connect: { id: options.guild } };
if (ticket.feedback.comment) { if (ticket.feedback.comment) {
ticket.feedback.comment = db_cryptr.encrypt(ticket.feedback.comment); ticket.feedback.comment = encryptIfExists(ticket.feedback.comment);
} }
ticket.feedback = { create: ticket.feedback }; ticket.feedback = { create: ticket.feedback };
} else { } else {
@ -146,7 +150,7 @@ for (const i in dump.tickets) {
ticket.questionAnswers = { ticket.questionAnswers = {
createMany: ticket.questionAnswers.map(answer => { createMany: ticket.questionAnswers.map(answer => {
delete answer.ticketId; delete answer.ticketId;
if (answer.value) answer.value = db_cryptr.encrypt(answer.value); if (answer.value) answer.value = encryptIfExists(answer.value);
return answer; return answer;
}), }),
}; };