Skip to content
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

mapping passthrough #31

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/schema/Mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"type": ["array", "null"],
"description": "The cast of this mapping object."
},
"passTrough": {
"passThrough": {
"type": ["boolean", "null"],
"default": true,
"description": "The passThrough of this mapping object."
Expand Down
6 changes: 3 additions & 3 deletions lib/Db/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Mapping extends Entity implements JsonSerializable
protected ?array $mapping = null;
protected ?array $unset = null;
protected ?array $cast = null;
protected ?bool $passTrough = null;
protected ?bool $passThrough = null;
protected ?DateTime $dateCreated = null;
protected ?DateTime $dateModified = null;

Expand All @@ -29,7 +29,7 @@ public function __construct() {
$this->addType('mapping', 'json');
$this->addType('unset', 'json');
$this->addType('cast', 'json');
$this->addType('passTrough', 'boolean');
$this->addType('passThrough', 'boolean');
$this->addType('dateCreated', 'datetime');
$this->addType('dateModified', 'datetime');
}
Expand Down Expand Up @@ -76,7 +76,7 @@ public function jsonSerialize(): array
'mapping' => $this->mapping,
'unset' => $this->unset,
'cast' => $this->cast,
'passTrough' => $this->passTrough,
'passThrough' => $this->passThrough,
'dateCreated' => isset($this->dateCreated) ? $this->dateCreated->format('c') : null,
'dateModified' => isset($this->dateModified) ? $this->dateModified->format('c') : null,
];
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/Version0Date20240826193657.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table->addColumn('mapping', Types::TEXT, ['notnull' => false]);
$table->addColumn('unset', Types::TEXT, ['notnull' => false]);
$table->addColumn('cast', Types::TEXT, ['notnull' => false]);
$table->addColumn('pass_trough', Types::BOOLEAN, ['notnull' => false]);
$table->addColumn('pass_through', Types::BOOLEAN, ['notnull' => false]);
$table->addColumn('date_created', Types::DATETIME, ['notnull' => true, 'default' => 'CURRENT_TIMESTAMP']);
$table->addColumn('date_modified', Types::DATETIME, ['notnull' => true, 'default' => 'CURRENT_TIMESTAMP']);
$table->setPrimaryKey(['id']);
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/MappingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function mapping(Mapping $mappingObject, array $input, bool $list = false

// Determine pass trough.
// Let's get the dot array based on https://github.com/adbario/php-dot-notation.
if ($mappingObject->getPassTrough()) {
if ($mappingObject->getPassThrough()) {
$dotArray = new Dot($input);
// @todo: error loging
// isset($this->style) === true && $this->style->info('Mapping *with* pass trough');
Expand Down Expand Up @@ -188,7 +188,7 @@ public function mapping(Mapping $mappingObject, array $input, bool $list = false
[
'input' => $input,
'output' => $output,
'passTrough' => $mappingObject->getPassTrough(),
'passThrough' => $mappingObject->getPassThrough(),
'mapping' => $mappingObject->getMapping(),
]
);
Expand Down
18 changes: 14 additions & 4 deletions src/entities/mapping/mapping.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { TMapping } from './mapping.types'

export const mockMappingData = (): TMapping[] => [
{
id: '5137a1e5-b54d-43ad-abd1-4b5bff5fcd3f',
id: 1,
uuid: '5137a1e5-b54d-43ad-abd1-4b5bff5fcd3f',
name: 'User Data Mapping',
version: '1.0.0',
description: 'Maps user data from source to target system',
Expand All @@ -12,10 +13,16 @@ export const mockMappingData = (): TMapping[] => [
{ source: 'lastName', target: 'family_name' },
{ source: 'email', target: 'email_address' },
],
passTrough: true,
passThrough: true,
reference: '',
unset: [],
cast: [],
dateCreated: '',
dateModified: '',
},
{
id: '4c3edd34-a90d-4d2a-8894-adb5836ecde8',
id: 2,
uuid: '5137a1e5-b54d-43ad-abd1-4b5bff5fcd3f',
name: 'Product Mapping',
version: '1.1.0',
description: 'Maps product data between systems',
Expand All @@ -24,12 +31,15 @@ export const mockMappingData = (): TMapping[] => [
{ source: 'productPrice', target: 'price' },
{ source: 'productDescription', target: 'description' },
],
passTrough: false,
passThrough: false,
unset: ['internal_id', 'created_by'],
cast: [
{ field: 'price', type: 'float' },
{ field: 'inStock', type: 'boolean' },
],
reference: '',
dateCreated: '',
dateModified: '',
},
]

Expand Down
46 changes: 28 additions & 18 deletions src/entities/mapping/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,48 @@ import { TMapping } from './mapping.types'

export class Mapping implements TMapping {

public id: string
public reference: string | null
public id: number
public uuid: string
public reference: string
public version: string
public name: string
public description: string | null
public description: string
public mapping: any[]
public unset: any[] | null
public cast: any[] | null
public passTrough: boolean | null
public dateCreated: string | null
public dateModified: string | null
public unset: any[]
public cast: any[]
public passThrough: boolean
public dateCreated: string
public dateModified: string

constructor(mapping: TMapping) {
this.id = mapping.id || ''
this.reference = mapping.reference || null
this.id = mapping.id || null
this.uuid = mapping.uuid || null
this.reference = mapping.reference || ''
this.version = mapping.version || '0.0.0'
this.name = mapping.name || ''
this.description = mapping.description || null
this.description = mapping.description || ''
this.mapping = mapping.mapping || []
this.unset = mapping.unset || null
this.cast = mapping.cast || null
this.passTrough = mapping.passTrough ?? true
this.dateCreated = mapping.dateCreated || null
this.dateModified = mapping.dateModified || null
this.unset = mapping.unset || []
this.cast = mapping.cast || []
this.passThrough = mapping.passThrough ?? false
this.dateCreated = mapping.dateCreated || ''
this.dateModified = mapping.dateModified || ''
}

public validate(): SafeParseReturnType<TMapping, unknown> {
const schema = z.object({
id: z.string().uuid(),
id: z.number().or(z.null()),
uuid: z.string().uuid().max(36).or(z.null()),
reference: z.string().max(255),
version: z.string().max(255),
name: z.string().max(255),
version: z.string(),
description: z.string(),
mapping: z.array(z.any()),
unset: z.array(z.any()),
cast: z.array(z.any()),
passThrough: z.boolean(),
dateCreated: z.string().or(z.literal('')),
dateModified: z.string().or(z.literal('')),
})

return schema.safeParse({ ...this })
Expand Down
17 changes: 9 additions & 8 deletions src/entities/mapping/mapping.types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export type TMapping = {
id?: string
reference?: string | null
id: number
uuid: string
reference: string
version: string
name: string
description?: string | null
description: string
mapping: any[]
unset?: any[] | null
cast?: any[] | null
passTrough?: boolean | null
dateCreated?: string | null
dateModified?: string | null
unset: any[]
cast: any[]
passThrough: boolean
dateCreated: string
dateModified: string
}
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Vue from 'vue'
import { PiniaVuePlugin } from 'pinia'
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip.js'
import pinia from './pinia.js'
import App from './App.vue'
Vue.mixin({ methods: { t, n } })

Vue.use(PiniaVuePlugin)
Vue.directive('tooltip', Tooltip)

new Vue(
{
Expand Down
75 changes: 53 additions & 22 deletions src/modals/Mapping/EditMapping.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { mappingStore, navigationStore } from '../../store/store.js'
@close="closeModal">
<div class="modalContent">
<h2>Mapping {{ mappingStore.mappingItem?.id ? 'Edit' : 'Add' }}</h2>

<NcNoteCard v-if="success" type="success">
<p>Mapping successfully added</p>
</NcNoteCard>
Expand All @@ -27,20 +28,32 @@ import { mappingStore, navigationStore } from '../../store/store.js'
id="description"
label="Description"
:value.sync="mappingItem.description" />

<span class="flex-container">
<NcCheckboxRadioSwitch
:checked.sync="mappingItem.passThrough">
Pass Through
</NcCheckboxRadioSwitch>
<a v-tooltip="'When turning passThrough on, all data from the original object is copied to the new object (passed through the mapper)'"
href="https://commongateway.github.io/CoreBundle/pages/Features/Mappings"
target="_blank">
<HelpCircleOutline :size="20" />
</a>
</span>
</div>
</form>

<NcButton
v-if="!success"
:disabled="loading"
type="primary"
@click="editMapping()">
<template #icon>
<NcLoadingIcon v-if="loading" :size="20" />
<ContentSaveOutline v-if="!loading" :size="20" />
</template>
Save
</NcButton>
<div v-if="!success" class="buttons">
<NcButton :disabled="loading"
type="primary"
@click="editMapping()">
<template #icon>
<NcLoadingIcon v-if="loading" :size="20" />
<ContentSaveOutline v-if="!loading" :size="20" />
</template>
Save
</NcButton>
</div>
</div>
</NcModal>
</template>
Expand All @@ -53,8 +66,10 @@ import {
NcNoteCard,
NcTextField,
NcTextArea,
NcCheckboxRadioSwitch,
} from '@nextcloud/vue'
import ContentSaveOutline from 'vue-material-design-icons/ContentSaveOutline.vue'
import HelpCircleOutline from 'vue-material-design-icons/HelpCircleOutline.vue'

export default {
name: 'EditMapping',
Expand All @@ -65,6 +80,7 @@ export default {
NcNoteCard,
NcTextField,
NcTextArea,
NcCheckboxRadioSwitch,
// Icons
ContentSaveOutline,
},
Expand All @@ -73,8 +89,9 @@ export default {
mappingItem: {
name: '',
description: '',
passThrough: false,
},
success: false,
success: null,
loading: false,
error: false,
hasUpdated: false,
Expand All @@ -101,30 +118,44 @@ export default {
closeModal() {
navigationStore.setModal(false)
clearTimeout(this.closeTimeoutFunc)
this.success = false
this.success = null
this.loading = false
this.error = false
this.hasUpdated = false
this.mappingItem = {
id: null,
name: '',
description: '',
passThrough: false,
}
},
async editMapping() {
this.loading = true
try {
await mappingStore.saveMapping(this.mappingItem)
// Close modal or show success message
this.success = true
this.loading = false

mappingStore.saveMapping({
...this.mappingItem,
}).then(({ response }) => {
this.success = response.ok
this.closeTimeoutFunc = setTimeout(this.closeModal, 2000)
} catch (error) {
this.loading = false
this.success = false
}).catch((error) => {
this.error = error.message || 'An error occurred while saving the mapping'
}
}).finally(() => {
this.loading = false
})
},
},
}
</script>

<style scoped>
.buttons {
display: flex;
gap: 10px;
}

.flex-container {
display: flex;
align-items: center;
gap: 10px;
}
</style>
3 changes: 3 additions & 0 deletions src/services/openLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function openLink(url, target = '') {
window.open(url, target)
}
Loading