liquid_feedback_frontend
view lib/token/sync.lua @ 1859:02c34183b6df
Fixed wrong filename in INSTALL file
| author | bsw | 
|---|---|
| date | Tue Nov 28 18:54:51 2023 +0100 (23 months ago) | 
| parents | e593570a23c5 | 
| children | 
 line source
     1 function _G.sync_token()
     2   local request = [[
     3     {
     4       "query": "query balanceNotificationMany($tokenSymbol: String) {
     5         balanceNotificationMany(filter: { token_symbol: $tokenSymbol }) {
     6             _id
     7             account_pk
     8             name
     9             token_symbol
    10             amount
    11             contractAddress
    12             owner
    13         }
    14       }",
    15       "variables": {
    16         "tokenSymbol": "]] .. config.token.token_name .. [["
    17       }
    18     }
    19   ]]
    21   local output, err, status = extos.pfilter(request, "curl", "--insecure", "-X", "POST", "-H", "Content-Type: application/json", "-H", "X-REQUEST-TYPE: GraphQL", "-d", "@-", config.token.graphql_url)
    23   local data = json.import(output)
    25   local privileged_member_ids = {}
    26   for i, entry in ipairs(data.data.balanceNotificationMany) do
    27     local member = Member:new_selector()
    28       :join("member_profile", nil, "member_profile.member_id = member.id")
    29       :add_where{ "member_profile.profile->>'" .. config.token.key_profile_field .."' = ?", entry.account_pk }
    30       :optional_object_mode()
    31       :exec()
    32     if member then
    33       privileged_member_ids[member.id] = true
    34       local privilege = Privilege:new_selector()
    35         :add_where{ "unit_id = ?", config.token.unit_id }
    36         :add_where{ "member_id = ?", member.id }
    37         :optional_object_mode()
    38         :exec()
    39       if not privilege then
    40         privilege = Privilege:new()
    41         privilege.unit_id = config.token.unit_id
    42         privilege.member_id = member.id
    43       end
    44       privilege.initiative_right = true
    45       privilege.voting_right = true
    46       privilege.weight = entry.amount
    47       privilege:save()
    48     end
    49   end
    51   local privileges = Privilege:new_selector()
    52     :add_where{ "unit_id = ?", config.token.unit_id }
    53     :exec()
    55   for i, privilege in ipairs(privileges) do
    56     if not privileged_member_ids[privilege.member_id] then
    57       privilege:destroy()
    58     end
    59   end
    62 end
