olsh/todoist-net

Temp ID assigned to new entities differ from Temp ID in thier add commands

Closed this issue · 0 comments

It appears that when we try to create a new entity (e.g. Project) with a TempId assigned to it, the temp_id sent to the Todoist API is not the same.

That results in the error "Invalid temporary id" when we attempt to use the new entity's TempId that we created for other commands, such as the following example in Todoist Docs:

An example that shows how temporary IDs can be used and referenced:

[
  { 
    "type": "project_add",
    "temp_id": "c7beb07f-b226-4eb1-bf63-30d782b07b1a",
    "args": {
      "name": "Shopping List"
    },
    "uuid": "ac417051-1cdc-4dc3-b4f8-14526d5bfd16"
  },
  {
    "type": "item_add",
    "temp_id": "43f7ed23-a038-46b5-b2c9-4abda9097ffa",
    "args": {
      "content": "Buy Milk",
      "project_id": "c7beb07f-b226-4eb1-bf63-30d782b07b1a"
    },
    "uuid": "849fff4e-8551-4abb-bd2a-838d092775d7"
  }
]

So for example, when we try to execute the following code it always fails with the message"Invalid temporary id":

var project = new Project("Shopping List")
{
    Id = new ComplexId(Guid.NewGuid()) // temp id that we create
};
var item = new Item("Buy milk")
{
    ProjectId = project.Id // temp id created
};

var transaction = client.CreateTransaction();

await transaction.Project.AddAsync(project);
await transaction.Items.AddAsync(item);

await transaction.CommitAsync(); // fails with "Invalid temporary id"

But from what I understand, it is intended that the TempId is automatically created (not by the user), and then it could be used in further commands, so, that makes the following code work:

var transaction = client.CreateTransaction();

// Add project to transaction first.
var project = new Project("Shopping List");
await transaction.Project.AddAsync(project);

// Then add item with the automatically created temp id.
var item = new Item("Buy milk")
{
    ProjectId = project.Id // temp id 
};
await transaction.Items.AddAsync(item);

// And finally, commit.
await transaction.CommitAsync(); // success

However, this approach does not cover many scenarios where users need to assign the TempId by hand, for example, when data is created by one service and the Todoist transaction is created by another.

I tried to fix this issue with a simple workaround temporarily, but I need to know first that this fix would not break any other code or result in unexpected behavior. I will create a pull request with the fix and link it to this issue shortly.