CreateIdentity inserts a new identity record into the database. It generates a unique IdentityID, sets the creation timestamp, and stores the identity metadata. Parameters: - identity: The identity object to be inserted. Returns: - The created identity object, or an error if the creation fails.
(identity model.Identity)
| 36 | // Returns: |
| 37 | // - The created identity object, or an error if the creation fails. |
| 38 | func (d Datasource) CreateIdentity(identity model.Identity) (model.Identity, error) { |
| 39 | // Marshal metadata into JSON format |
| 40 | metaDataJSON, err := json.Marshal(identity.MetaData) |
| 41 | if err != nil { |
| 42 | return identity, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to marshal metadata", err) |
| 43 | } |
| 44 | |
| 45 | // Generate a unique identity ID and set the creation timestamp |
| 46 | identity.IdentityID = model.GenerateUUIDWithSuffix("idt") |
| 47 | identity.CreatedAt = time.Now() |
| 48 | |
| 49 | // Insert the identity record into the database |
| 50 | _, err = d.Conn.ExecContext(context.Background(), ` |
| 51 | INSERT INTO ledgerforge.identity (identity_id, identity_type, first_name, last_name, other_names, gender, dob, email_address, phone_number, nationality, organization_name, category, street, country, state, post_code, city, created_at, meta_data) |
| 52 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) |
| 53 | `, identity.IdentityID, identity.IdentityType, identity.FirstName, identity.LastName, identity.OtherNames, identity.Gender, identity.DOB, identity.EmailAddress, identity.PhoneNumber, identity.Nationality, identity.OrganizationName, identity.Category, identity.Street, identity.Country, identity.State, identity.PostCode, identity.City, identity.CreatedAt, metaDataJSON) |
| 54 | // Handle any errors that occur during insertion |
| 55 | if err != nil { |
| 56 | return identity, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to create identity", err) |
| 57 | } |
| 58 | |
| 59 | // Return the created identity |
| 60 | return identity, nil |
| 61 | } |
| 62 | |
| 63 | // GetIdentityByID retrieves an identity from the database based on the given identity ID. |
| 64 | // It starts a transaction, executes a query to fetch the identity details, and commits the transaction upon success. |