Skip to content

Commit

Permalink
Fix job creation with full transaction for jobs and job_grades tables
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpredator committed Oct 2, 2024
1 parent 09b8019 commit a66e1e1
Showing 1 changed file with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@

local NOTIFY_TYPES = {
INFO = "^5[%s]^7-^6[INFO]^7 %s",
SUCCESS = "^5[%s]^7-^2[SUCCESS]^7 %s",
ERROR = "^5[%s]^7-^1[ERROR]^7 %s"
ERROR = "^5[%s]^7-^1[ERROR]^7 %s",
}

local function doesJobAndGradesExist(name, grades)
if not ESX.Jobs[name] then
return false
return false
end

for _, grade in ipairs(grades) do
if not ESX.DoesJobExist(name, grade.grade) then
return false
end
end

return true
end

local function generateTransactionQueries(name,grades)
local queries = {}
for _, grade in ipairs(grades) do
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, '{}', '{}'}
}
if not ESX.DoesJobExist(name, grade.grade) then
return false
end
end

return queries
return true
end

local function generateNewJobTable(name, label, grades)
Expand All @@ -40,14 +27,14 @@ local function generateNewJobTable(name, label, grades)
return job
end

local function notify(notifyType,resourceName,message,...)
local function notify(notifyType, resourceName, message, ...)
local formattedMessage = string.format(message, ...)

if not NOTIFY_TYPES[notifyType] then
return print(NOTIFY_TYPES.INFO:format(resourceName,formattedMessage))
return print(NOTIFY_TYPES.INFO:format(resourceName, formattedMessage))
end

return print(NOTIFY_TYPES[notifyType]:format(resourceName,formattedMessage))
return print(NOTIFY_TYPES[notifyType]:format(resourceName, formattedMessage))
end

--- Create Job at Runtime
Expand All @@ -58,45 +45,49 @@ function ESX.CreateJob(name, label, grades)
local currentResourceName = GetInvokingResource()
local success = false

if not name or name == '' then
notify("ERROR",currentResourceName, 'Missing argument `name`')
return
if not name or name == "" then
notify("ERROR", currentResourceName, "Missing argument `name`")
return success
end
if not label or label == '' then
notify("ERROR",currentResourceName, 'Missing argument `label`')
return

if not label or label == "" then
notify("ERROR", currentResourceName, "Missing argument `label`")
return success
end

if not grades or not next(grades) then
notify("ERROR",currentResourceName, 'Missing argument `grades`')
return
notify("ERROR", currentResourceName, "Missing argument `grades`")
return success
end

local currentJobExist = doesJobAndGradesExist(name, grades)

if currentJobExist then
notify("ERROR",currentResourceName, 'Job or grades already exists: `%s`', name)
return
notify("ERROR", currentResourceName, "Job or grades already exists: `%s`", name)
return success
end

MySQL.insert('INSERT IGNORE INTO jobs (name, label) VALUES (?, ?)', {name, label}, function(jobId)
if jobId == nil or jobId == 0 then
notify("ERROR",currentResourceName, 'Failed to insert job: `%s`', name)
return
end
local queries = {
{ query = "INSERT INTO jobs (name, label) VALUES (?, ?)", values = { name, label } },
}

local queries = generateTransactionQueries(name, grades)
for _, grade in ipairs(grades) do
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, "{}", "{}" },
}
end

success = exports.oxmysql:transaction_async(queries)

if not success then
notify("ERROR", currentResourceName, "Failed to insert one or more grades for job: `%s`", name)
return success
end

MySQL.transaction(queries, function(results)
success = results
if not results then
notify("ERROR",currentResourceName, 'Failed to insert one or more grades for job: `%s`', name)
return
end
ESX.Jobs[name] = generateNewJobTable(name, label, grades)

ESX.Jobs[name] = generateNewJobTable(name,label,grades)
notify("SUCCESS",currentResourceName, 'Job created successfully: `%s`', name)
end)
end)
notify("SUCCESS", currentResourceName, "Job created successfully: `%s`", name)

return success
end

0 comments on commit a66e1e1

Please sign in to comment.