delegate method

AttendeeDelegatedResult delegate({
  1. required String fromEmail,
  2. String? toEmail,
  3. AttendeeProperty? to,
  4. bool rsvp = true,
  5. ParticipantStatus? toStatus,
  6. String? comment,
})

Delegates this calendar from the user with fromEmail to the user toEmail / to.

The optional parameters rsvp and toStatus are ignored when to is specified. Optionally explain the reason in the comment.

Implementation

AttendeeDelegatedResult delegate({
  required String fromEmail,
  String? toEmail,
  AttendeeProperty? to,
  bool rsvp = true,
  ParticipantStatus? toStatus,
  String? comment,
}) {
  assert(!(toEmail == null && to == null),
      'Either to or toEmail must be specified.');
  final forDelegatee = copy() as VCalendar;
  forDelegatee.method = Method.request;
  final event =
      forDelegatee.children.firstWhereOrNull((ev) => ev is VEvent) as VEvent?;
  if (event != null) {
    if (comment != null) {
      event.comment = comment;
    }
    event.removeAttendeeWithEmail(fromEmail);
    event.addAttendee(
      AttendeeProperty.create(
        attendeeEmail: fromEmail,
        participantStatus: ParticipantStatus.delegated,
        delegatedToEmail: toEmail,
        delegatedToUri: to?.uri,
      )!,
    );
    to ??= AttendeeProperty.create(
      attendeeEmail: toEmail,
      participantStatus: toStatus,
      rsvp: rsvp,
      delegatedFromEmail: fromEmail,
    )!;
    event.removeAttendeeWithUri(to.uri);
    event.addAttendee(to);
  }
  final forOrganizer = replyWithParticipantStatus(
    ParticipantStatus.delegated,
    attendeeEmail: fromEmail,
    delegatedToEmail: to!.email,
  );
  return AttendeeDelegatedResult(forDelegatee, forOrganizer);
}