# Compras

### Relacionamentos

Toda compra é relacionada a empresa responsável, que é definida como o `org_id` da mesma. Essa relação é estabelecida com base no CNPJ presente na compra.

O gestor responsável pela ação também é relacionado à compra, com base no email presente na compra.

```json
{"id": "101", "sk": "0", "name": "EDUSEG", "cnpj": "15608435000190", "org_id": "100"}
{"id": "101", "sk": "CREATED_BY", "name": "Sérgio", "email": "sergio@somosbeta.com.br", "user_id": "123"}
{"id": "101", "sk": "SLOT", "status": "PENDING", "mode": "BATCH"}
{"id": "101", "sk": "SLOT#ENROLLMENT#9omWNKymwU5U4aeun6mWzZ", "status": "SUCCESS"}
{"id": "101", "sk": "SLOT#ENROLLMENT#12", "status": "ROLLBACK"}
{"id": "101", "sk": "ITEMS", "items": []}
{"id": "101", "sk": "NFSE", "nfse": "123"}
```

Quando o responsável é uma pessoa física (CPF).

```json
{"id": "20", "sk": "0", "name": "Sérgio", "email": "sergio@somosbeta.com.br", "cpf": "07879819908", "user_id": "321"}
{"id": "20", "sk": "SLOT", "status": "PENDING", "mode": "STANDALONE"}
{"id": "20", "sk": "SLOT#ENROLLMENT#1123", "status": "SUCCESS"}
```

# Usuários

```json
{"id": "89ada056-141c-4c8a-b442-86ab799f0bb7", "sk": "0", "name": "Sérgio R Siqueira", "cpf": "07879819908", "email": "sergio@somosbeta.com.br", "email_verified": true, "emails": ["osergiosiqueira@gmail.com"]}
{"id": "89ada056-141c-4c8a-b442-86ab799f0bb7", "sk": "EMAIL#sergio@somosbeta.com.br", "email_verified": true, "email_primary": true, "mx_record_exists": true}
{"id": "89ada056-141c-4c8a-b442-86ab799f0bb7", "sk": "EMAIL#osergiosiqueira@gmail.com", "email_verified": false, "email_primary": false, "mx_record_exists": true}
```

# Matrículas

```json
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "0", "course": {"id": "10", "name": "pytest"}, "org_id": "100"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "METADATA#COURSE", "access_period": 360, "cert": {"exp_interval": 365}}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "CERT", "s3_uri": "", "expires_at": "2026-03-01T11:07:32.762178-03:00", "created_at": "2025-04-06T11:07:32.762178-03:00"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "METADATA#DEDUPLICATION_WINDOW", "offset_days": 90}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "ORG", "org_id": "100", "name": "EDUSEG"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "CREATED_BY", "user_id": "202", "name": "Tiago Maciel"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "KONVIVA", "user_id": 122, "class_id": 123, "enrollment_id": 1239}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "STARTED", "started_at": "2025-04-06T11:07:32.762178-03:00"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "COMPLETED", "completed_at": "2025-04-06T11:07:32.762178-03:00"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "FAILED", "failed_at": "2025-04-06T11:07:32.762178-03:00"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "CANCELED", "canceled_at": "2025-04-06T11:07:32.762178-03:00", "canceled_by": {"id": "", "name": ""}}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "ARCHIVED", "archived_at": "2025-04-06T11:07:32.762178-03:00"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "EXPIRED", "expired_at": "2025-04-06T11:07:32.762178-03:00"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "RELATION#ORDER", "order_id": "101"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "LOCK", "hash": "1e67e29464877783e49e07fb7d9dd372", "ttl": 1874507093}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "SCHEDUE#REMINDER_NO_ACCESS_AFTER_3_DAYS", "ttl": 1874507093}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "SCHEDUE#REMINDER_NO_ACTIVITY_AFTER_7_DAYS", "ttl": 1874507093}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "SCHEDUE#REMINDER_ACCESS_PERIOD_BEFORE_15_DAYS", "ttl": 1874507093}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "SCHEDUE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS", "ttl": 1874507093}
```

### Vagas disponíveis

```json
{"id": "SLOT#ORG#100", "sk": "ORDER#101#ENROLLMENT#faa8a547-bb9b-4103-bd8c-8fbe96b4056f", "course": {"name": "pytest"}}
{"id": "SLOT#ORG#100", "sk": "ORDER#101#ENROLLMENT#afffbdde-fe58-4df7-b4d5-7553a571d32a", "course": {"name": "pytest"}}
```

### Emails/eventos agendados

Quando uma matrícula é criada, também é agendados emails/eventos.

-   `REMINDER_NO_ACCESS_AFTER_3_DAYS` se o usuário não acessar o curso 3 dias após a criação.
-   `REMINDER_NO_ACTIVITY_AFTER_7_DAYS` 7 dias após a última atividade do usuário no curso.
-   `REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS` 30 dias antes do período de acesso ao curso terminar.
-   `REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS` se houver certificado, avisa 30 dias antes do certificado expirar.
-   `SET_AS_ARCHIVED` após o certificado expirar, a matrícula será marcada como **arquivada (ARCHIVED)**.
-   `SET_AS_EXPIRED` se não houver certificado e o período de acesso for atingido, a matrícula será marcada com **expirada (EXPIRED)**.

```json
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "SCHEDULE#REMINDER_NO_ACCESS_3_DAYS", "name": "Sérgio R Siqueira", "email": "osergiosiqueira@gmail.com", "ttl": 1874507093}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "SCHEDULE#SET_AS_EXPIRED", "name": "Sérgio R Siqueira", "email": "osergiosiqueira@gmail.com", "ttl": 1874507093}
```

Quando o status da matrícula for alterado para `COMPLETED`, os eventos `SET_AS_EXPIRED` e `REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS` serão _removidos_ a adicionado o evento `SET_AS_ARCHIVED`.

```json
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "0", "ttl": 1874507093}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "SCHEDULE#SET_AS_ARCHIVED", "ttl": 1874507093}
```

### Proteção contra duplicação

A proteção contra duplicação é um mecanismo que impede que o gestor matricule o mesmo colaborador no curso por engano.

O gestor pode definir quantos dias antes do fim do acesso ao curso a proteção será removida automaticamente.

Se um certificado for emitido para a matrícula, o período de proteção será recalculado conforme a validade do certificado.

```json
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "METADATA#DEDUPLICATION_WINDOW", "offset_days": 90}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "LOCK", "hash": "1e67e29464877783e49e07fb7d9dd372", "ttl": 1767625113}
{"id": "LOCK", "sk": "1e67e29464877783e49e07fb7d9dd372", "ttl": 1767625113}
```

### Política de cancelamento

Apenas matrículas com `CANCEL_POLICY` podem ser canceladas.

Se houver `METADATA#SOURCE`, deve ser devolvido.

```json
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "0"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "CANCEL_POLICY"}
{"id": "9omWNKymwU5U4aeun6mWzZ", "sk": "METADATA#SOURCE", "slot": {"id": "SLOT#ORG#123", "sk": "ORDER#1221#ENROLLMENT#9omWNKymwU5U4aeun6mWzZ"}}
```

# Cursos

### Valores contratados

```json
{"id": "CUSTOM_PRICING#ORG#96e523b9-a404-4860-a737-edf412c3da52", "sk": "COURSE#439e9a43-ab92-469a-a849-b6e824370f80", "unit_price": 149, "created_at": "2025-04-06T11:07:32.762178-03:00"}
{"id": "CUSTOM_PRICING#ORG#96e523b9-a404-4860-a737-edf412c3da52", "sk": "COURSE#f10c3283-7722-41c6-ba5d-222f9f4f48af", "unit_price": 149, "created_at": "2025-04-06T11:07:32.762178-03:00"}
```

# Webhooks

```json
{"id": "USER#*#EVENT#INSERT_ADMIN", "sk": "https://n8n.eduseg.com.br/webhook/2bc4a7cd-94d0-4214-b330-4261da87dc95"}
{"id": "USER#*#EVENT#OVERDUE_PAYMENT", "sk": "https://n8n.eduseg.com.br/webhook/2bc4a7cd-94d0-4214-b330-4261da87dc95"}
{"id": "REQUEST#URL_HASH#de2815bdfc4238d446eb0f686ff51c0d", "sk": "2025-07-24T21:45:29.642855", "request_payload": {"id": "123", "name": "Sérgio Siqueira"}}
```

`URL_HASH` é o hash MD5 da string `USER#{user_id}#EVENT#{event_name}#URL#{url}'`
