# Pipedrive Deal Creation

## Objective

Turn a qualified lead or purchase into a Pipedrive person, organization, and deal.

A flow that finds or creates the person and organization, then opens a deal associated with both in the configured pipeline.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Resolve the person and the organization first, matching the person on the normalized email and the organization on its domain or name, and only then create the deal that references them.
2. Make the three creates a recoverable sequence. Record what succeeded after each step, so a failure at the deal stage resumes from the existing person and organization instead of creating a second set.
3. Let an admin configure the pipeline, stage, owner, currency, and how the deal value is derived, and validate every one of those against the account before the mapping can be saved.
4. Derive a deterministic deal key from the app record that triggered it, and check for an existing deal under that key before creating one. Repeated events for the same signal must find the deal, not open another.
5. Do not resolve stages, pipelines, or users by their display names at run time. Names are edited freely; store stable identifiers and re-validate them, and pause the mapping with a named error when one has disappeared.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Creating a deal before its person and organization exist leaves orphaned deals with no one to call. Resolve or create both, confirm the associations, and only then open the deal.
- Pipeline, stage, owner, currency, and value must all be chosen deliberately. A deal dropped into the wrong pipeline at a default stage with no value is invisible in forecasting and will be worked by the wrong person.
- The same app event fires more than once through retries, webhook redelivery, and user impatience. Without a deterministic key derived from the source record, one purchase opens three deals and inflates the pipeline.
- Saved mappings reference stages and users that get deleted or deactivated. Detect the missing reference, pause the mapping with a message naming what is gone, and hold events rather than assigning to whatever remains.
- A partial failure — person created, organization created, deal rejected — must be resumable from where it stopped. Persist progress per step so the retry completes the sequence instead of starting a new one.
- Rate limits and token expiry both interrupt a multi-step sequence mid-way. Back off and resume from the recorded step, and mark the connection broken when reauthorization is required rather than retrying indefinitely.
- Organization matching by name is unreliable — punctuation, suffixes, and abbreviations all differ. Prefer the domain, and where the match is ambiguous, create the deal against the person and flag the organization for a human.
- When the provider is unavailable, the app's own conversion must still complete. Queue the sequence, show its status on the source record, and give operators a way to retry it by hand.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Person and organization are resolved or created before any deal references them.
- [ ] Repeated events for one source record produce exactly one deal.
- [ ] Pipeline, stage, owner, currency, and value are configured and validated against the account.
- [ ] Deleted stages or deactivated users pause the mapping with a named error rather than defaulting.
- [ ] A partial failure resumes from the last completed step without duplicating earlier objects.
- [ ] Ambiguous organization matches are flagged rather than guessed.
- [ ] The app-side conversion completes and the sync status is visible and retryable.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
