init-parse-schema.ps1 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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("#")) {
  8. return
  9. }
  10. if ($line -match "^([^=]+)=(.*)$") {
  11. $map[$Matches[1].Trim()] = $Matches[2].Trim()
  12. }
  13. }
  14. return $map
  15. }
  16. function Field {
  17. param(
  18. [string]$Type,
  19. [string]$TargetClass = ""
  20. )
  21. if ($Type -eq "Pointer") {
  22. return @{ type = $Type; targetClass = $TargetClass }
  23. }
  24. return @{ type = $Type }
  25. }
  26. $envPath = Join-Path $PSScriptRoot "..\.env"
  27. $envMap = Read-DotEnv -Path $envPath
  28. $required = @("PARSE_APP_ID", "PARSE_REST_API_KEY", "PARSE_MASTER_KEY", "PARSE_SERVER_URL")
  29. $missing = $required | Where-Object { -not $envMap.ContainsKey($_) -or [string]::IsNullOrWhiteSpace($envMap[$_]) }
  30. if ($missing.Count -gt 0) {
  31. throw "Missing Parse env: $($missing -join ', ')"
  32. }
  33. $baseUrl = $envMap["PARSE_SERVER_URL"].TrimEnd("/")
  34. $headers = @{
  35. "X-Parse-Application-Id" = $envMap["PARSE_APP_ID"]
  36. "X-Parse-REST-API-Key" = $envMap["PARSE_REST_API_KEY"]
  37. "X-Parse-Master-Key" = $envMap["PARSE_MASTER_KEY"]
  38. }
  39. $authenticatedClp = @{
  40. find = @{ requiresAuthentication = $true }
  41. get = @{ requiresAuthentication = $true }
  42. count = @{ requiresAuthentication = $true }
  43. create = @{ requiresAuthentication = $true }
  44. update = @{ requiresAuthentication = $true }
  45. delete = @{}
  46. addField = @{}
  47. }
  48. $schemas = @(
  49. @{
  50. className = "_User"
  51. description = "用户表"
  52. fields = @{
  53. name = (Field "String")
  54. role = (Field "String")
  55. phone = (Field "String")
  56. avatarUrl = (Field "String")
  57. status = (Field "String")
  58. company = (Field "Pointer" "Company")
  59. }
  60. },
  61. @{
  62. className = "Company"
  63. description = "客户企业表"
  64. fields = @{
  65. name = (Field "String")
  66. creditCode = (Field "String")
  67. contactName = (Field "String")
  68. contactPhone = (Field "String")
  69. email = (Field "String")
  70. serviceLevel = (Field "String")
  71. address = (Field "String")
  72. remark = (Field "String")
  73. status = (Field "String")
  74. mockBatch = (Field "String")
  75. }
  76. },
  77. @{
  78. className = "Ticket"
  79. description = "工单表"
  80. fields = @{
  81. ticketNo = (Field "String")
  82. title = (Field "String")
  83. description = (Field "String")
  84. category = (Field "String")
  85. priority = (Field "String")
  86. impactScope = (Field "String")
  87. status = (Field "String")
  88. company = (Field "Pointer" "Company")
  89. creator = (Field "Pointer" "_User")
  90. assignee = (Field "Pointer" "_User")
  91. firstRespondedAt = (Field "Date")
  92. resolvedAt = (Field "Date")
  93. closedAt = (Field "Date")
  94. isOverdue = (Field "Boolean")
  95. slaDeadline = (Field "Date")
  96. companyName = (Field "String")
  97. ownerName = (Field "String")
  98. mockBatch = (Field "String")
  99. }
  100. },
  101. @{
  102. className = "TicketComment"
  103. description = "工单沟通记录"
  104. fields = @{
  105. ticket = (Field "Pointer" "Ticket")
  106. author = (Field "Pointer" "_User")
  107. content = (Field "String")
  108. visibility = (Field "String")
  109. mockBatch = (Field "String")
  110. }
  111. },
  112. @{
  113. className = "TicketAttachment"
  114. description = "工单附件"
  115. fields = @{
  116. ticket = (Field "Pointer" "Ticket")
  117. uploader = (Field "Pointer" "_User")
  118. fileName = (Field "String")
  119. fileUrl = (Field "String")
  120. fileType = (Field "String")
  121. fileSize = (Field "Number")
  122. businessType = (Field "String")
  123. mockBatch = (Field "String")
  124. }
  125. },
  126. @{
  127. className = "TicketStatusLog"
  128. description = "工单状态流转日志"
  129. fields = @{
  130. ticket = (Field "Pointer" "Ticket")
  131. operator = (Field "Pointer" "_User")
  132. fromStatus = (Field "String")
  133. toStatus = (Field "String")
  134. remark = (Field "String")
  135. mockBatch = (Field "String")
  136. }
  137. },
  138. @{
  139. className = "SlaRule"
  140. description = "SLA 规则"
  141. fields = @{
  142. priority = (Field "String")
  143. responseMinutes = (Field "Number")
  144. resolveHours = (Field "Number")
  145. alertBeforeMinutes = (Field "Number")
  146. enabled = (Field "Boolean")
  147. mockBatch = (Field "String")
  148. }
  149. },
  150. @{
  151. className = "KnowledgeCategory"
  152. description = "知识库分类"
  153. fields = @{
  154. name = (Field "String")
  155. sortOrder = (Field "Number")
  156. enabled = (Field "Boolean")
  157. mockBatch = (Field "String")
  158. }
  159. },
  160. @{
  161. className = "KnowledgeArticle"
  162. description = "知识库文章"
  163. fields = @{
  164. title = (Field "String")
  165. category = (Field "Pointer" "KnowledgeCategory")
  166. summary = (Field "String")
  167. content = (Field "String")
  168. tags = (Field "Array")
  169. visibility = (Field "String")
  170. status = (Field "String")
  171. author = (Field "Pointer" "_User")
  172. viewCount = (Field "Number")
  173. referenceCount = (Field "Number")
  174. mockBatch = (Field "String")
  175. }
  176. },
  177. @{
  178. className = "SatisfactionReview"
  179. description = "满意度评价"
  180. fields = @{
  181. ticket = (Field "Pointer" "Ticket")
  182. customer = (Field "Pointer" "_User")
  183. serviceScore = (Field "Number")
  184. speedScore = (Field "Number")
  185. solutionScore = (Field "Number")
  186. averageScore = (Field "Number")
  187. content = (Field "String")
  188. followedUp = (Field "Boolean")
  189. mockBatch = (Field "String")
  190. }
  191. },
  192. @{
  193. className = "Notification"
  194. description = "站内通知"
  195. fields = @{
  196. receiver = (Field "Pointer" "_User")
  197. title = (Field "String")
  198. content = (Field "String")
  199. type = (Field "String")
  200. read = (Field "Boolean")
  201. relatedTicket = (Field "Pointer" "Ticket")
  202. mockBatch = (Field "String")
  203. }
  204. },
  205. @{
  206. className = "AuditLog"
  207. description = "操作日志"
  208. fields = @{
  209. operator = (Field "Pointer" "_User")
  210. module = (Field "String")
  211. action = (Field "String")
  212. targetClass = (Field "String")
  213. targetId = (Field "String")
  214. ip = (Field "String")
  215. success = (Field "Boolean")
  216. detail = (Field "Object")
  217. mockBatch = (Field "String")
  218. }
  219. }
  220. )
  221. function Test-SchemaExists {
  222. param([string]$ClassName)
  223. try {
  224. Invoke-RestMethod -Method Get -Uri "$baseUrl/schemas/$ClassName" -Headers $headers | Out-Null
  225. return $true
  226. }
  227. catch {
  228. if ($_.Exception.Response -and [int]$_.Exception.Response.StatusCode -eq 404) {
  229. return $false
  230. }
  231. if ($_.ErrorDetails -and $_.ErrorDetails.Message -match '"code"\s*:\s*103') {
  232. return $false
  233. }
  234. if ($_.Exception.Message -match 'does not exist') {
  235. return $false
  236. }
  237. throw
  238. }
  239. }
  240. foreach ($schema in $schemas) {
  241. $exists = Test-SchemaExists -ClassName $schema.className
  242. $body = @{
  243. fields = $schema.fields
  244. }
  245. if ($schema.className -ne "_User") {
  246. $body.classLevelPermissions = $authenticatedClp
  247. }
  248. $json = $body | ConvertTo-Json -Depth 12
  249. $method = if ($exists) { "Put" } else { "Post" }
  250. Invoke-RestMethod -Method $method -Uri "$baseUrl/schemas/$($schema.className)" -Headers $headers -ContentType "application/json" -Body $json | Out-Null
  251. $verb = if ($exists) { "Updated" } else { "Created" }
  252. Write-Host "$verb $($schema.className) - $($schema.description)"
  253. }
  254. Write-Host "Parse schema initialization finished. Class count: $($schemas.Count)"