Fixed database ghosted unused-options bug

Fixed a problem where unused ticket options would stay in the options database in certain circumstances. Even when there is no ticket that uses the option anymore.
This commit is contained in:
DJj123dj
2024-12-07 00:34:49 +01:00
parent 1e2321313f
commit a037d76060
2 changed files with 10 additions and 12 deletions
+1 -7
View File
@@ -1,7 +1 @@
[
{"category":"openticket:last-version","key":"openticket:version","value":"v4.0.0"},
{"category":"openticket:last-version","key":"openticket:api","value":"v1.0.0"},
{"category":"openticket:last-version","key":"openticket:transcripts","value":"v2.0.0"},
{"category":"openticket:last-version","key":"openticket:livestatus","value":"v2.0.0"},
{"category":"openticket:last-version","key":"openticket:last-version","value":"v4.0.0"}
]
[]
+9 -5
View File
@@ -133,13 +133,17 @@ export const loadDatabaseCleanersCode = async () => {
}))
//OPTION DATABASE CLEANER
openticket.code.add(new api.ODCode("openticket:option-database-cleaner",10,() => {
openticket.code.add(new api.ODCode("openticket:option-database-cleaner",10,async () => {
//delete all unused options (async)
openticket.options.getAll().forEach(async (option) => {
if (await optionDatabase.exists("openticket:used-option",option.id.value) && !openticket.tickets.getAll().some((ticket) => ticket.option.id.value == option.id.value)){
await optionDatabase.delete("openticket:used-option",option.id.value)
for (const option of (await optionDatabase.getCategory("openticket:used-option") ?? [])){
if (!openticket.options.exists(option.key)){
//remove because option isn't loaded into memory (0 tickets require it)
await optionDatabase.delete("openticket:used-option",option.key)
}else if (!openticket.tickets.getAll().some((ticket) => ticket.option.id.value == option.key)){
//remove when loaded into memory (0 tickets require it)
await optionDatabase.delete("openticket:used-option",option.key)
}
}
})
}))
//USER DATABASE CLEANER (full async/parallel because it takes a lot of time)