seed-mock-data.ps1 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. $ErrorActionPreference = "Stop"
  2. function Read-DotEnv {
  3. param([string]$Path)
  4. $map = @{}
  5. Get-Content -Encoding UTF8 $Path | ForEach-Object {
  6. $line = $_.Trim()
  7. if ($line.Length -eq 0 -or $line.StartsWith("#")) { return }
  8. if ($line -match "^([^=]+)=(.*)$") {
  9. $map[$Matches[1].Trim()] = $Matches[2].Trim()
  10. }
  11. }
  12. return $map
  13. }
  14. function Invoke-Parse {
  15. param(
  16. [string]$Method,
  17. [string]$Path,
  18. [object]$Body = $null
  19. )
  20. $headers = @{
  21. "X-Parse-Application-Id" = $envMap["PARSE_APP_ID"]
  22. "X-Parse-REST-API-Key" = $envMap["PARSE_REST_API_KEY"]
  23. "X-Parse-Master-Key" = $envMap["PARSE_MASTER_KEY"]
  24. }
  25. $json = if ($null -ne $Body) { $Body | ConvertTo-Json -Depth 16 } else { $null }
  26. Invoke-RestMethod -Method $Method -Uri "$baseUrl$Path" -Headers $headers -ContentType "application/json" -Body $json
  27. }
  28. function Pointer {
  29. param([string]$ClassName, [string]$ObjectId)
  30. return @{ "__type" = "Pointer"; className = $ClassName; objectId = $ObjectId }
  31. }
  32. function AclForUsers {
  33. param([string[]]$UserIds)
  34. $acl = @{}
  35. foreach ($userId in $UserIds) {
  36. if ($userId) {
  37. $acl[$userId] = @{ read = $true; write = $true }
  38. }
  39. }
  40. return $acl
  41. }
  42. function Create-Object {
  43. param([string]$ClassName, [object]$Body)
  44. Invoke-Parse -Method "Post" -Path "/classes/$ClassName" -Body $Body
  45. }
  46. function Create-User {
  47. param(
  48. [string]$Username,
  49. [string]$Password,
  50. [string]$Name,
  51. [string]$Role,
  52. [string]$Phone,
  53. [object]$CompanyPointer = $null
  54. )
  55. $body = @{
  56. username = $Username
  57. email = $Username
  58. password = $Password
  59. name = $Name
  60. role = $Role
  61. phone = $Phone
  62. status = "enabled"
  63. mockBatch = $batch
  64. }
  65. if ($CompanyPointer) {
  66. $body.company = $CompanyPointer
  67. }
  68. try {
  69. return Invoke-Parse -Method "Post" -Path "/users" -Body $body
  70. }
  71. catch {
  72. Write-Host "Skip existing or invalid user: $Username"
  73. $where = [uri]::EscapeDataString((@{ username = $Username } | ConvertTo-Json -Compress))
  74. $result = Invoke-Parse -Method "Get" -Path "/users?where=$where"
  75. return $result.results[0]
  76. }
  77. }
  78. $envPath = Join-Path $PSScriptRoot "..\.env"
  79. $envMap = Read-DotEnv -Path $envPath
  80. $baseUrl = $envMap["PARSE_SERVER_URL"].TrimEnd("/")
  81. $batch = "servicepilot-mock-20260601"
  82. $password = "Test@123456"
  83. Write-Host "Seeding ServicePilot mock data..."
  84. $companies = @(
  85. @{ name = "Huadong Yunqi Tech"; contactName = "Zhou Yuan"; contactPhone = "13800001001"; email = "contact01@example.com"; serviceLevel = "enterprise"; address = "Shanghai Pudong"; status = "enabled"; mockBatch = $batch },
  86. @{ name = "Beichen Manufacturing"; contactName = "Liu Na"; contactPhone = "13800001002"; email = "contact02@example.com"; serviceLevel = "professional"; address = "Tianjin Binhai"; status = "enabled"; mockBatch = $batch },
  87. @{ name = "Huilian Data Service"; contactName = "Chen Lin"; contactPhone = "13800001003"; email = "contact03@example.com"; serviceLevel = "standard"; address = "Hangzhou Xihu"; status = "enabled"; mockBatch = $batch }
  88. )
  89. $companyObjects = @()
  90. foreach ($company in $companies) {
  91. $created = Create-Object -ClassName "Company" -Body $company
  92. $company.objectId = $created.objectId
  93. $companyObjects += $company
  94. Write-Host "Created Company $($company.name)"
  95. }
  96. $support = Create-User -Username "support@servicepilot.test" -Password $password -Name "Support Wang" -Role "support" -Phone "13900002001"
  97. $engineer = Create-User -Username "engineer@servicepilot.test" -Password $password -Name "Engineer Chen" -Role "engineer" -Phone "13900002002"
  98. $admin = Create-User -Username "admin@servicepilot.test" -Password $password -Name "System Admin" -Role "admin" -Phone "13900002003"
  99. $customers = @()
  100. for ($i = 0; $i -lt $companyObjects.Count; $i++) {
  101. $company = $companyObjects[$i]
  102. $customer = Create-User -Username "customer$($i + 1)@servicepilot.test" -Password $password -Name "$($company.name) Customer" -Role "customer" -Phone "1390000300$($i + 1)" -CompanyPointer (Pointer "Company" $company.objectId)
  103. $customers += $customer
  104. }
  105. $titles = @(
  106. "Monthly report export failed",
  107. "New employee cannot login",
  108. "API returns 500 occasionally",
  109. "Data sync job delayed",
  110. "Customer portal loads slowly",
  111. "Attachment preview failed",
  112. "Menu not refreshed after permission change",
  113. "Knowledge search is inaccurate",
  114. "Customer import failed",
  115. "SLA timeout reminder missing"
  116. )
  117. $categories = @("software", "account", "data", "other")
  118. $priorities = @("low", "medium", "high", "urgent")
  119. $statuses = @("pending", "processing", "confirming", "resolved")
  120. for ($i = 0; $i -lt 60; $i++) {
  121. $company = $companyObjects[$i % $companyObjects.Count]
  122. $creator = $customers[$i % $customers.Count]
  123. $assignee = if ($i % 4 -eq 0) { $support } else { $engineer }
  124. $status = $statuses[$i % $statuses.Count]
  125. $priority = $priorities[$i % $priorities.Count]
  126. $ticketNo = "TK-20260601-$("{0:D3}" -f ($i + 1))"
  127. $acl = AclForUsers @($creator.objectId, $assignee.objectId, $support.objectId, $admin.objectId)
  128. $ticket = @{
  129. ticketNo = $ticketNo
  130. title = $titles[$i % $titles.Count]
  131. description = "Mock ticket for CRUD, ACL isolation, API tests, UI automation, and performance tests."
  132. category = $categories[$i % $categories.Count]
  133. priority = $priority
  134. impactScope = if ($i % 3 -eq 0) { "company" } elseif ($i % 3 -eq 1) { "department" } else { "single-user" }
  135. status = $status
  136. company = Pointer "Company" $company.objectId
  137. companyName = $company.name
  138. creator = Pointer "_User" $creator.objectId
  139. assignee = Pointer "_User" $assignee.objectId
  140. ownerName = $assignee.name
  141. isOverdue = ($i % 9 -eq 0)
  142. slaDeadline = @{ "__type" = "Date"; iso = (Get-Date).AddHours(4 - ($i % 8)).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ") }
  143. mockBatch = $batch
  144. ACL = $acl
  145. }
  146. $createdTicket = Create-Object -ClassName "Ticket" -Body $ticket
  147. if ($i -lt 12) {
  148. Create-Object -ClassName "TicketComment" -Body @{
  149. ticket = Pointer "Ticket" $createdTicket.objectId
  150. author = Pointer "_User" $assignee.objectId
  151. content = "Issue received and investigation started."
  152. visibility = "public"
  153. mockBatch = $batch
  154. ACL = $acl
  155. } | Out-Null
  156. }
  157. }
  158. $slaRules = @(
  159. @{ priority = "urgent"; responseMinutes = 15; resolveHours = 4; alertBeforeMinutes = 30; enabled = $true; mockBatch = $batch },
  160. @{ priority = "high"; responseMinutes = 30; resolveHours = 8; alertBeforeMinutes = 60; enabled = $true; mockBatch = $batch },
  161. @{ priority = "medium"; responseMinutes = 120; resolveHours = 24; alertBeforeMinutes = 120; enabled = $true; mockBatch = $batch },
  162. @{ priority = "low"; responseMinutes = 240; resolveHours = 48; alertBeforeMinutes = 240; enabled = $true; mockBatch = $batch }
  163. )
  164. foreach ($rule in $slaRules) {
  165. Create-Object -ClassName "SlaRule" -Body $rule | Out-Null
  166. }
  167. $category = Create-Object -ClassName "KnowledgeCategory" -Body @{ name = "FAQ"; sortOrder = 1; enabled = $true; mockBatch = $batch }
  168. foreach ($articleTitle in @("Report export troubleshooting", "First login troubleshooting", "API 500 diagnosis", "Attachment preview troubleshooting")) {
  169. Create-Object -ClassName "KnowledgeArticle" -Body @{
  170. title = $articleTitle
  171. category = Pointer "KnowledgeCategory" $category.objectId
  172. summary = "Mock article for knowledge search and ticket reference."
  173. content = "Step 1: confirm symptom. Step 2: collect logs. Step 3: resolve and update ticket."
  174. tags = @("test", "ticket", "knowledge")
  175. visibility = "customer"
  176. status = "published"
  177. author = Pointer "_User" $admin.objectId
  178. viewCount = 10
  179. referenceCount = 2
  180. mockBatch = $batch
  181. } | Out-Null
  182. }
  183. Write-Host "Mock users:"
  184. Write-Host " customer1@servicepilot.test / $password"
  185. Write-Host " customer2@servicepilot.test / $password"
  186. Write-Host " customer3@servicepilot.test / $password"
  187. Write-Host " support@servicepilot.test / $password"
  188. Write-Host " engineer@servicepilot.test / $password"
  189. Write-Host " admin@servicepilot.test / $password"
  190. Write-Host "Seed finished. Ticket count: 60"