Skip to content

Commit 87b9974

Browse files
committed
fix(scripts): don't encrypt/decrypt missing values
1 parent 8fded79 commit 87b9974

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

scripts/export.mjs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const file_path = join(process.cwd(), './user/dumps', `${hash}.dump`);
2121
const file_cryptr = new Cryptr(options.guild);
2222
const db_cryptr = new Cryptr(process.env.ENCRYPTION_KEY);
2323

24+
function decryptIfExists(encrypted) {
25+
if (encrypted) return db_cryptr.decrypt(encrypted);
26+
return null;
27+
}
28+
2429
fse.ensureDirSync(join(process.cwd(), './user/dumps'));
2530

2631
let spinner = ora('Connecting').start();
@@ -73,30 +78,30 @@ dump.tickets = await prisma.ticket.findMany({
7378
where: { guildId: options.guild },
7479
});
7580
dump.tickets = dump.tickets.map(ticket => {
76-
if (ticket.topic) ticket.topic = db_cryptr.decrypt(ticket.topic);
81+
if (ticket.topic) ticket.topic = decryptIfExists(ticket.topic);
7782

7883
ticket.archivedChannels = ticket.archivedChannels.map(channel => {
79-
channel.name = db_cryptr.decrypt(channel.name);
84+
channel.name = decryptIfExists(channel.name);
8085
return channel;
8186
});
8287

8388
ticket.archivedMessages = ticket.archivedMessages.map(message => {
84-
message.content = db_cryptr.decrypt(message.content);
89+
message.content = decryptIfExists(message.content);
8590
return message;
8691
});
8792

8893
ticket.archivedUsers = ticket.archivedUsers.map(user => {
89-
user.displayName = db_cryptr.decrypt(user.displayName);
90-
user.username = db_cryptr.decrypt(user.username);
94+
user.displayName = decryptIfExists(user.displayName);
95+
user.username = decryptIfExists(user.username);
9196
return user;
9297
});
9398

9499
if (ticket.feedback?.comment) {
95-
ticket.feedback.comment = db_cryptr.decrypt(ticket.feedback.comment);
100+
ticket.feedback.comment = decryptIfExists(ticket.feedback.comment);
96101
}
97102

98103
ticket.questionAnswers = ticket.questionAnswers.map(answer => {
99-
if (answer.value) answer.value = db_cryptr.decrypt(answer.value);
104+
if (answer.value) answer.value = decryptIfExists(answer.value);
100105
return answer;
101106
});
102107

scripts/import.mjs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const hash = createHash('sha256').update(options.guild).digest('hex');
2121
const file_cryptr = new Cryptr(options.guild);
2222
const db_cryptr = new Cryptr(process.env.ENCRYPTION_KEY);
2323

24+
function encryptIfExists(plain_text) {
25+
if (plain_text) return db_cryptr.encrypt(plain_text);
26+
return null;
27+
}
2428

2529
let spinner = ora('Connecting').start();
2630

@@ -98,21 +102,21 @@ for (const i in dump.tickets) {
98102
const ticket = dump.tickets[i];
99103
ticket.category = { connect: { id: category_map[ticket.categoryId] } };
100104

101-
if (ticket.topic) ticket.topic = db_cryptr.encrypt(ticket.topic);
105+
if (ticket.topic) ticket.topic = encryptIfExists(ticket.topic);
102106

103107
ticket.archivedChannels = {
104108
create: ticket.archivedChannels.map(channel => {
105109
delete channel.ticketId;
106-
channel.name = db_cryptr.encrypt(channel.name);
110+
channel.name = encryptIfExists(channel.name);
107111
return channel;
108112
}),
109113
};
110114

111115
ticket.archivedUsers = {
112116
create: ticket.archivedUsers.map(user => {
113117
delete user.ticketId;
114-
user.displayName = db_cryptr.encrypt(user.displayName);
115-
user.username = db_cryptr.encrypt(user.username);
118+
user.displayName = encryptIfExists(user.displayName);
119+
user.username = encryptIfExists(user.username);
116120
return user;
117121
}),
118122
};
@@ -125,7 +129,7 @@ for (const i in dump.tickets) {
125129
};
126130

127131
const archivedMessages = ticket.archivedMessages.map(message => {
128-
message.content = db_cryptr.encrypt(message.content);
132+
message.content = encryptIfExists(message.content);
129133
return message;
130134
});
131135
ticket.archivedMessages = undefined;
@@ -135,7 +139,7 @@ for (const i in dump.tickets) {
135139
delete ticket.feedback.guildId;
136140
ticket.feedback.guild = { connect: { id: options.guild } };
137141
if (ticket.feedback.comment) {
138-
ticket.feedback.comment = db_cryptr.encrypt(ticket.feedback.comment);
142+
ticket.feedback.comment = encryptIfExists(ticket.feedback.comment);
139143
}
140144
ticket.feedback = { create: ticket.feedback };
141145
} else {
@@ -146,7 +150,7 @@ for (const i in dump.tickets) {
146150
ticket.questionAnswers = {
147151
createMany: ticket.questionAnswers.map(answer => {
148152
delete answer.ticketId;
149-
if (answer.value) answer.value = db_cryptr.encrypt(answer.value);
153+
if (answer.value) answer.value = encryptIfExists(answer.value);
150154
return answer;
151155
}),
152156
};

0 commit comments

Comments
 (0)