Implemented transfer and invite to jobs and notifications

This commit is contained in:
Ferdinand Thiessen 2021-11-24 21:47:14 +01:00
parent 6c32aae7b4
commit 512e68f1ed
8 changed files with 148 additions and 85 deletions

8
src/api.d.ts vendored
View File

@ -13,11 +13,13 @@ declare namespace FG {
id: number;
name: string;
}
interface Invite {
interface Invitation {
id: number;
job_id: number;
invitee_id: string;
sender_id: string;
inviter_id: string;
transferee_id?: string;
transferee: User;
}
interface Job {
id: number;
@ -25,9 +27,9 @@ declare namespace FG {
end?: Date;
type: JobType | number;
comment?: string;
locked: boolean;
services: Array<Service>;
required_services: number;
locked: boolean;
}
interface JobType {
id: number;

View File

@ -32,7 +32,7 @@
flat
color="primary"
label="Eintragen"
@click="enrollForJob"
@click="assignJob()"
/>
<q-btn v-if="isEnrolled && !modelValue.locked" flat color="secondary" label="Optionen">
<q-menu auto-close>
@ -44,7 +44,7 @@
<q-item-section>Tauschen</q-item-section>
</q-item>
<q-separator />
<q-item clickable @click="signOutFromJob">
<q-item clickable @click="assignJob(false)">
<q-item-section class="text-negative">Austragen</q-item-section>
</q-item>
</q-list>
@ -115,46 +115,21 @@ export default defineComponent({
)
);
async function enrollForJob() {
async function assignJob(assign = true) {
const newService: FG.Service = {
userid: mainStore.currentUser.userid,
is_backup: false,
value: 1,
value: assign ? 1 : -1,
};
try {
await store.assignToJob(props.modelValue.id, newService);
const job = Object.assign({}, props.modelValue)
job.services.push(newService)
const job = await store.assignToJob(props.modelValue.id, newService);
emit('update:modelValue', job);
} catch (error) {
console.warn(error);
quasar.notify({
group: false,
type: 'negative',
message: 'Fehler beim Eintragen als Dienst',
timeout: 10000,
progress: true,
actions: [{ icon: 'mdi-close', color: 'white' }],
});
}
}
async function signOutFromJob() {
const newService: FG.Service = {
userid: mainStore.currentUser.userid,
is_backup: false,
value: -1,
};
try {
await store.assignToJob(props.modelValue.id, newService);
const job = Object.assign({}, props.modelValue)
job.services.push(newService)
emit('update:modelValue', job);
} catch (error) {
console.warn(error);
quasar.notify({
group: false,
type: 'negative',
message: 'Fehler beim Austragen als Dienst',
message: 'Fehler beim Ein- oder Austragen als Dienst',
timeout: 10000,
progress: true,
actions: [{ icon: 'mdi-close', color: 'white' }],
@ -166,19 +141,18 @@ export default defineComponent({
quasar.dialog({
component: TransferInviteDialog,
componentProps: {
invite: isInvite,
isInvite: isInvite,
job: props.modelValue,
},
});
}
return {
assignJob,
canInvite,
enrollForJob,
isEnrolled,
isFull,
invite: () => invite(true),
signOutFromJob,
transfer: () => invite(false),
typeName,
userDisplay,

View File

@ -65,10 +65,9 @@ export default defineComponent({
);
function invite() {
store
.sendInvite(props.job, invitees.value, !props.isInvite)
.then(() => onDialogOK())
.catch(() => onDialogCancel());
void store
.invite(props.job, invitees.value, !props.isInvite ? mainStore.currentUser : undefined)
.then(() => onDialogOK());
}
return {

32
src/events.d.ts vendored
View File

@ -1,9 +1,25 @@
declare namespace FG {
export interface RecurrenceRule {
frequency: string;
interval: number;
count?: number;
until?: Date;
weekdays?: Array<number>;
}
import { FG_Plugin } from "@flaschengeist/types";
export interface RecurrenceRule {
frequency: string;
interval: number;
count?: number;
until?: Date;
weekdays?: Array<number>;
}
interface InvitationData {
invitation: number;
}
interface InvitationResponseData {
event: number,
job: number,
invitee: string
}
export interface EventNotification extends FG_Plugin.Notification {
data: {
type: number
} & (InvitationData | InvitationResponseData);
}

View File

@ -1,6 +1,39 @@
import { innerRoutes, privateRoutes } from './routes';
import { FG_Plugin } from '@flaschengeist/types';
import { defineAsyncComponent } from 'vue';
import { EventNotification, InvitationData, InvitationResponseData } from './events';
import { useEventStore } from './store';
const EventTypes = {
_mask_: 0xf0,
invitation: 0x00,
invite: 0x01,
transfer: 0x02,
invitation_response: 0x10,
invitation_accepted: 0x10,
invitation_rejected: 0x11,
};
function transpile(msg: FG_Plugin.Notification) {
const message = msg as EventNotification;
message.icon = 'mdi-calendar';
if ((message.data.type & EventTypes._mask_) === EventTypes.invitation) {
message.accept = () => {
const store = useEventStore();
return store.acceptInvitation((<InvitationData>message.data).invitation);
};
message.reject = () => {
const store = useEventStore();
return store.rejectInvitation((<InvitationData>message.data).invitation);
};
message.link = { name: 'events-requests' };
} else if ((message.data.type & EventTypes._mask_) === EventTypes.invitation_response) {
message.link = {name: 'events-single-view', params: {id: (<InvitationResponseData>message.data).event}}
}
return message;
}
const plugin: FG_Plugin.Plugin = {
id: 'dev.flaschengeist.events',
@ -9,6 +42,7 @@ const plugin: FG_Plugin.Plugin = {
internalRoutes: privateRoutes,
requiredModules: [['events']],
version: '0.0.1',
notification: transpile,
widgets: [
{
priority: 0,

View File

@ -1,25 +1,37 @@
<template>
<q-page padding class="fit row justify-center content-start items-start q-gutter-sm">
<EditEvent v-model="event" />
<EditEvent :model-value="event" />
</q-page>
</template>
<script lang="ts">
import { onBeforeMount, defineComponent, ref } from 'vue';
import { defineComponent, onMounted, ref, watch, PropType } from 'vue';
import EditEvent from '../components/management/EditEvent.vue';
import { useEventStore } from '../store';
import { useRoute } from 'vue-router';
export default defineComponent({
components: { EditEvent },
setup() {
const route = useRoute();
props: {
id: {
type: String as PropType<number | string>,
default: undefined,
},
},
setup(props) {
const store = useEventStore();
const event = ref<FG.Event | undefined>(undefined);
onBeforeMount(async () => {
if ('id' in route.params && typeof route.params.id === 'string')
event.value = await store.getEvent(parseInt(route.params.id));
});
const event = ref<FG.Event>();
function loadEvent(id?: string | number) {
if (id != event.value?.id)
if (id === undefined) event.value = undefined;
else
void store
.getEvent(typeof id === 'number' ? id : parseInt(id))
.then((e) => (event.value = e));
}
watch(props, (v) => loadEvent(v.id));
onMounted(() => loadEvent(props.id));
return {
event,

View File

@ -7,8 +7,8 @@ export const innerRoutes: FG_Plugin.MenuRoute[] = [
icon: 'mdi-briefcase',
permissions: ['user'],
route: {
path: 'schedule',
name: 'schedule',
path: 'events',
name: 'events',
redirect: { name: 'schedule-overview' },
},
children: [
@ -39,8 +39,8 @@ export const innerRoutes: FG_Plugin.MenuRoute[] = [
icon: 'mdi-account-switch',
shortcut: false,
route: {
path: 'schedule-requests',
name: 'schedule-requests',
path: 'events-requests',
name: 'events-requests',
component: () => import('../pages/EventRequests.vue'),
},
},
@ -50,13 +50,14 @@ export const innerRoutes: FG_Plugin.MenuRoute[] = [
export const privateRoutes: FG_Plugin.NamedRouteRecordRaw[] = [
{
name: 'new-event',
path: 'new-event',
redirect: { name: 'schedule-management' },
name: 'events-single-view',
path: 'events/:id',
component: () => import('../pages/EventPage.vue'),
props: true
},
{
name: 'events-edit',
path: 'schedule/:id/edit',
path: 'events/:id/edit',
component: () => import('../pages/EventPage.vue'),
},
];

View File

@ -2,11 +2,18 @@ import { api, isAxiosError } from '@flaschengeist/api';
import { defineStore } from 'pinia';
import { EditableEvent } from './models';
/**
* Convert JSON decoded Job to real job (fix Date object)
*/
function fixJob(job: FG.Job) {
job.start = new Date(job.start);
if (job.end) job.end = new Date(job.end);
return job;
}
/**
* Convert JSON decoded Event to real Event object (fix Date object)
*/
function fixEvent(event: FG.Event) {
event.start = new Date(event.start);
if (event.end) event.end = new Date(event.end);
@ -77,18 +84,6 @@ export const useEventStore = defineStore({
.then(({ data }) => this.eventTypes.push(data));
},
async removeEvent(id: number) {
try {
await api.delete(`/events/${id}`);
const idx = this.templates.findIndex((v) => v.id === id);
if (idx !== -1) this.templates.splice(idx, 1);
} catch (e) {
if (isAxiosError(e, 404)) return false;
throw e;
}
return true;
},
removeEventType(id: number) {
return api
.delete(`/events/event-types/${id}`)
@ -135,8 +130,16 @@ export const useEventStore = defineStore({
}
},
async assignToJob(jobId: number, service: FG.Service) {
return api.post<FG.Job>(`/events/jobs/${jobId}/assign`, service);
async removeEvent(id: number) {
try {
await api.delete(`/events/${id}`);
const idx = this.templates.findIndex((v) => v.id === id);
if (idx !== -1) this.templates.splice(idx, 1);
} catch (e) {
if (isAxiosError(e, 404)) return false;
throw e;
}
return true;
},
async addEvent(event: EditableEvent) {
@ -157,11 +160,33 @@ export const useEventStore = defineStore({
}
},
async sendInvite(job: FG.Job, invitees: FG.User[], isInvite = true) {
return api.post<FG.Event>('/events/transfer', {
async assignToJob(jobId: number, service: FG.Service) {
return api
.post<FG.Job>(`/events/jobs/${jobId}/assign`, service)
.then(({ data }) => fixJob(data));
},
/**
* Send invite to job or transfer to other user
* @param job Job to invite to
* @param invitees Users to invite
* @param isTransfer set to True to transfer service instead of invite
*/
async invite(job: FG.Job, invitees: FG.User[], transferee: FG.User | undefined = undefined) {
return api.post<FG.Invitation[]>('/events/invitations', {
job: job.id,
receiver: invitees,
is_invite: isInvite,
invitees: invitees.map((v) => v.userid),
transferee: transferee,
});
},
async rejectInvitation(invite: FG.Invitation | number) {
return api.delete(`/events/invitations/${typeof invite === 'number' ? invite : invite.id}`);
},
async acceptInvitation(invite: FG.Invitation | number) {
return api.put(`/events/invitations/${typeof invite === 'number' ? invite : invite.id}`, {
accept: true,
});
},
},