Update map of maps

Hi guys. Have a nice day!
I have this custom types:

  // ticket definition
  record ticket = 
    { owner    : address,
      quantity : int }

  // meeting definition
  record meeting =
    { name        : string,
      date        : string,
      time        : string,
      capacity    : int,
      address1    : string,
      address2    : string,
      image       : string,
      ticketPrice : int,
      opened      : bool,
      tickets     : map(address, ticket) }

and this state:

record state = 
  { meetings : map(int, meeting) }

in this function i try to add/modify ticket to meeting.tickets

payable stateful entrypoint buyTicket(meetingId: int, quantity: int) =
  let meeting = getMeeting(meetingId)

    // validate meeting is opened
    if (!meeting.opened) abort("El evento ha finalizado o está cerrado")

    let total = meeting.ticketPrice * quantity

    // validate has money to buy
    if (Call.value < total) abort("No ha enviado el monto suficiente para el total de entradas")

    // validate meeting has tickets availables
    if (meeting.capacity < quantity) abort("No puede comprar mas de la cantidad disponible")

    let updatedQty = meeting.capacity - quantity
    let updatedStatus = updatedQty != 0

    let tickets = meeting.tickets

    let ownTicketsQty = switch(Map.lookup(Call.caller, tickets))
      None      => 0
      Some(tkt) => tkt.quantity + quantity

    let ticket = 
      { owner = Call.caller,
        quantity = ownTicketsQty }
    
    tickets{ [Call.caller] = ticket }

    let updatedMeetings = state.meetings{ 
      [meetingId].capacity = updatedQty,
      [meetingId].opened = updatedStatus,
      [meetingId].tickets = tickets }

    // update state
    put(state{ meetings = updatedMeetings })

but when i try to get meeting info i get this response:

Result: {"address1":"address line 1","address2":"address line 
2","capacity":11,"date":"today","image":"image.utl","name":"first 
meetup","opened":true,"ticketPrice":1,"tickets":[],"time":"now"}

in this response tickets is a empty array “[]” instead of a map “{}” with values.

1 Like

i tryed this code too:

let tickets = meeting.tickets

let ownTicketsQty = switch(Map.lookup(Call.caller, tickets))
  None      => 0
  Some(tkt) => tkt.quantity + quantity

let ticket = 
  { owner = Call.caller,
    quantity = ownTicketsQty }
    
let updatedTickets = tickets{ [Call.caller] = ticket }

let updatedMeetings = state.meetings{ 
  [meetingId].capacity = updatedQty,
  [meetingId].opened = updatedStatus,
  [meetingId].tickets = updatedTickets }
1 Like

in this line your update the tickets map to insert the new ticket, which is the correct syntax, but the map is not updated in a mutable way, but returns the updated map as return value, you never assign the updated map as return value anywhere.

What you need to do is

let insertedTickets = tickets{ [Call.caller] = ticket }

let updatedMeetings = state.meetings{ 
  [meetingId].capacity = updatedQty,
  [meetingId].opened = updatedStatus,
  [meetingId].tickets = insertedTickets }