-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add emails endpoints #47
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dzięki za sprawną robotę, kilka mniejszych komentarzy
app/controllers/emails_controller.ts
Outdated
const emails = await Email.query() | ||
.where('event_id', eventId) | ||
.preload('participants', async (query) => { | ||
await query.pivotColumns(['status']) | ||
}) | ||
|
||
const emailSummaries = emails.map((email) => ({ | ||
id: email.id, | ||
name: email.name, | ||
pending: email.participants.filter((p) => p.$extras.pivot_status === 'pending').length, | ||
sent: email.participants.filter((p) => p.$extras.pivot_status === 'sent').length, | ||
failed: email.participants.filter((p) => p.$extras.pivot_status === 'failed').length, | ||
})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kamilramocki spróbuj użyć withCount z custom query w którym podajesz status (albo może grupowanie tez zadziała?)
https://lucid.adonisjs.com/docs/model-query-builder#withcount
app/controllers/emails_controller.ts
Outdated
if (data.trigger) { | ||
const validTriggers = ['participant_registered', 'form_filled', 'attribute_changed'] | ||
if (!validTriggers.includes(data.trigger)) { | ||
return response.status(400).send({ message: 'Invalid trigger provided.' }) | ||
} | ||
|
||
if ( | ||
(data.trigger === 'form_filled' || data.trigger === 'attribute_changed') && | ||
!data.triggerValue | ||
) { | ||
return response | ||
.status(400) | ||
.send({ message: 'Trigger value is required for the selected trigger.' }) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Przenieśmy to do walidatora
Może się przydać: https://vinejs.dev/docs/conditional_validation#requiredwhen
app/controllers/emails_controller.ts
Outdated
* Create an email for a specific event | ||
*/ | ||
async store({ params, request, response }: HttpContext) { | ||
const eventId = Number(params.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Myślę, że mimo wszystko powinniśmy sprawdzić czy dany event istnieje i wtedy może utworzyć event.related('emails').create(data)
app/controllers/emails_controller.ts
Outdated
if (data.trigger) { | ||
const validTriggers = ['participant_registered', 'form_filled', 'attribute_changed'] | ||
if (!validTriggers.includes(data.trigger)) { | ||
return response.status(400).send({ message: 'Invalid trigger provided.' }) | ||
} | ||
|
||
if ( | ||
(data.trigger === 'form_filled' || data.trigger === 'attribute_changed') && | ||
!data.triggerValue | ||
) { | ||
return response | ||
.status(400) | ||
.send({ message: 'Trigger value is required for the selected trigger.' }) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tu może być ten sam walidator co wyżej
const participant = await Participant.findOrFail(params.id); | ||
|
||
const emailId = request.input('email_id') as number; | ||
|
||
const pivotData = request.only(['send_at', 'send_by', 'status']) as { | ||
send_at?: string; | ||
send_by?: string; | ||
status?: string; | ||
}; | ||
|
||
await participant.related('emails').attach({ [emailId]: pivotData }); | ||
return response.status(201).send({ message: 'Email successfully attached.' }); | ||
} | ||
|
||
/** | ||
* Detach email from participant | ||
*/ | ||
async detachEmail({ params, request, response }: HttpContext) { | ||
const participant = await Participant.findOrFail(params.id); | ||
|
||
const emailId = request.input('email_id') as number; | ||
|
||
await participant.related('emails').detach([emailId]); | ||
return response.status(200).send({ message: 'Email successfully detached.' }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To rozwiążemy potem, bo nie będzie czegoś takiego jak detach email, będzie sendEmail i jak już zostanie wysłany to nie można go usunąć
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kamilramocki już wygląda to dobrze. Jak zrobisz testy i dokumentację to będzie można mergować
No description provided.