# An import that skips duplicates

An import that skips a record already there recovers from the create's
`conflict:` failure instead of passing it on. A `match` handles a call's
failures, and a write isn't a call, so the create goes in an action of its
own, which passes its failure on with `attempt`, and the import matches on
each call to it ([A failed action inside another](/docs/when-a-write-is-refused.md#a-failed-action-inside-another)).
Each call runs in a savepoint, so a refused create is undone alone and the
import carries on. A `match` around the create itself is
`failure.attempt_required`, whose next step says the same.

Here contacts come over from an old system, and two contacts never share
an email:

```jadpo
use contacts/types { ContactName }

/// Someone in the address book. Two contacts never share an email.
entity Contact {
    id: Uuid
    name: ContactName
    email: Email {
        unique: true
    }
}
```

`add_contact` makes one contact and fails with `EmailTaken` when the email
is there:

```jadpo
use contacts/entities { Contact }
use contacts/types { ContactRow }
use contacts/failures { EmailTaken }

/// Adds one contact, failing if its email is taken.
action add_contact(row: ContactRow) fails EmailTaken -> Contact {
    return attempt Contact.create {
        set: {
            name: row.name
            email: row.email
        }
        conflict: {
            Contact.email: EmailTaken
        }
    }
}
```

The import counts what each call gave. A row whose email an earlier row of
the same import added is skipped too, since that contact is there by then:

```jadpo
use contacts/types { ContactImport, ImportCounts }
use contacts/failures { EmailTaken }
use contacts/actions { add_contact }

/// Adds each contact whose email isn't taken yet, and skips the rest,
/// including a second row with an email the import already added.
action import_contacts(input: ContactImport) -> ImportCounts {
    var mut imported: Int = 0
    var mut skipped: Int = 0
    for row in input.contacts {
        match add_contact(row) {
            success -> {
                imported = imported + 1
            }
            EmailTaken -> {
                skipped = skipped + 1
            }
        }
    }
    return ImportCounts {
        imported: imported
        skipped: skipped
    }
}
```

```jadpo
use contacts/entities { Contact }
use contacts/types { ContactImport, ContactRow, ImportCounts }
use contacts/actions { add_contact }

test "an import skips an email already there, and a repeat of its own" {
    var ada: Contact = attempt add_contact(
        ContactRow {
            name: "Ada"
            email: "ada@example.com"
        }
    )
    var counts: ImportCounts = import_contacts(
        ContactImport {
            contacts: [
                ContactRow {
                    name: "Ada Lovelace"
                    email: "ada@example.com"
                },
                ContactRow {
                    name: "Bo"
                    email: "bo@example.com"
                },
                ContactRow {
                    name: "Bo again"
                    email: "bo@example.com"
                }
            ]
        }
    )
    assert counts == ImportCounts {
        imported: 1
        skipped: 2
    }
    var kept: Contact = attempt Contact.get(ada.id)
    assert kept.name == "Ada"
}
```

`attempt add_contact(row)` in the loop would pass `EmailTaken` on instead,
ending the import and undoing every contact it had added.
