mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-28 17:01:14 +00:00
fix(es_extended/server/modules/createJob): cache only the grades that were inserted
A grade that already exists in the database is skipped for the insert, but it was still written to ESX.Jobs. A second CreateJob call therefore replaced the stored name, label and salary in memory while the row itself kept the old values, and the two only agreed again after a restart. CreateJob now caches just the grades it inserted. Label and type of an existing job come from the database for the same reason. The grade lookup compares strings, so passing grade "1" for an existing grade 1 no longer inserts a second row.
This commit is contained in:
@@ -52,14 +52,17 @@ function ESX.CreateJob(name, label, grades, jobType)
|
||||
jobType = "civ"
|
||||
end
|
||||
|
||||
local jobExists = MySQL.scalar.await('SELECT 1 FROM `jobs` WHERE `name` = ?', { name }) ~= nil
|
||||
local existingJob = MySQL.single.await('SELECT `label`, `type` FROM `jobs` WHERE `name` = ?', { name })
|
||||
local jobExists = existingJob ~= nil
|
||||
local existingGrades = {}
|
||||
|
||||
if jobExists then
|
||||
label, jobType = existingJob.label, existingJob.type
|
||||
|
||||
local rows = MySQL.query.await('SELECT `grade` FROM `job_grades` WHERE `job_name` = ?', { name })
|
||||
|
||||
for i = 1, #(rows or {}) do
|
||||
existingGrades[rows[i].grade] = true
|
||||
existingGrades[tostring(rows[i].grade)] = true
|
||||
end
|
||||
end
|
||||
|
||||
@@ -72,8 +75,11 @@ function ESX.CreateJob(name, label, grades, jobType)
|
||||
}
|
||||
end
|
||||
|
||||
local newGrades = {}
|
||||
|
||||
for _, grade in pairs(grades) do
|
||||
if not existingGrades[grade.grade] then
|
||||
if not existingGrades[tostring(grade.grade)] then
|
||||
newGrades[#newGrades + 1] = grade
|
||||
queries[#queries + 1] = {
|
||||
query = 'INSERT INTO job_grades (job_name, grade, name, label, salary, skin_male, skin_female) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
values = { name, grade.grade, grade.name, grade.label, grade.salary, grade.skin_male and json.encode(grade.skin_male) or '{}', grade.skin_female and json.encode(grade.skin_female) or '{}' }
|
||||
@@ -93,7 +99,7 @@ function ESX.CreateJob(name, label, grades, jobType)
|
||||
return success
|
||||
end
|
||||
|
||||
ESX.Jobs[name] = generateNewJobTable(name, label, grades, jobType)
|
||||
ESX.Jobs[name] = generateNewJobTable(name, label, newGrades, jobType)
|
||||
|
||||
notify("SUCCESS", currentResourceName, 'Job created successfully: `%s`', name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user