Skip to content

Commit eda0d23

Browse files
chore: update code for more optimized way
1 parent d4a8394 commit eda0d23

File tree

2 files changed

+79
-60
lines changed

2 files changed

+79
-60
lines changed

frontend-dev/src/components/AllIntegrations/Line/LineCommonFunc.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const delFieldMap = (index, _, setConf, type) => {
8888
})
8989
}
9090

91-
const FIELD_TYPE_GROUPS = {
91+
const FIELD_CATEGORIES = {
9292
sticker: ['sticker_id', 'package_id'],
9393
image: ['originalContentUrl', 'previewImageUrl'],
9494
audio: ['originalContentUrl', 'duration'],
@@ -97,8 +97,8 @@ const FIELD_TYPE_GROUPS = {
9797
emoji: ['emojis_id', 'product_id', 'index']
9898
}
9999

100-
const getFieldTypeGroup = lineFormField => {
101-
for (const [type, fields] of Object.entries(FIELD_TYPE_GROUPS)) {
100+
const getFieldCategory = lineFormField => {
101+
for (const [type, fields] of Object.entries(FIELD_CATEGORIES)) {
102102
if (fields.includes(lineFormField)) {
103103
return type
104104
}
@@ -118,7 +118,7 @@ const generateGroupId = (fieldType, existingFieldMap) => {
118118
const existingGroups = [
119119
...new Set(
120120
existingFieldMap
121-
.filter(field => getFieldTypeGroup(field.lineFormField) === fieldType)
121+
.filter(field => getFieldCategory(field.lineFormField) === fieldType)
122122
.map(field => field.groupId)
123123
)
124124
]
@@ -141,7 +141,7 @@ export const addFieldMap = (i, confTmp, setConf, FieldMappings, mapKey) => {
141141
newConf[mapKey].splice(i, 0, ...newFieldMap)
142142
return newConf
143143
}
144-
let fieldType = getFieldTypeGroup(FieldMappings[0].value)
144+
let fieldType = getFieldCategory(FieldMappings[0].value)
145145
let groupId = generateGroupId(fieldType, newConf[mapKey])
146146
const newFieldMap = FieldMappings.map(field => ({
147147
formField: '',
@@ -238,7 +238,7 @@ export const generateMappedField = (fields, fieldType = null) => {
238238
if (requiredFlds.length) {
239239
const typeToGroupId = {}
240240
return requiredFlds.map(f => {
241-
const actualFieldType = fieldType || getFieldTypeGroup(f.value)
241+
const actualFieldType = fieldType || getFieldCategory(f.value)
242242
if (!typeToGroupId[actualFieldType]) {
243243
typeToGroupId[actualFieldType] = `${actualFieldType}_1`
244244
}

includes/Actions/Line/RecordApiHelper.php

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -67,94 +67,62 @@ private function buildMessages($details, $values)
6767
}
6868

6969
if (!empty($details->sendSticker) && !empty($details->sticker_field_map)) {
70+
$stickerRequiredFields = ['sticker_id', 'package_id'];
7071
$stickers = $this->processGrouped(
7172
$values,
7273
$details->sticker_field_map,
73-
['sticker_id', 'package_id'],
74-
function ($data) {
75-
return [
76-
'type' => 'sticker',
77-
'packageId' => $data['package_id'],
78-
'stickerId' => $data['sticker_id'],
79-
];
80-
}
74+
$stickerRequiredFields,
75+
[$this, 'transformSticker']
8176
);
8277
$messages = array_merge($messages, $stickers);
8378
}
8479

8580
if (!empty($details->sendImage) && !empty($details->image_field_map)) {
81+
$imageRequiredFields = ['originalContentUrl'];
8682
$images = $this->processGrouped(
8783
$values,
8884
$details->image_field_map,
89-
['originalContentUrl'],
90-
function ($data) {
91-
return array_filter([
92-
'type' => 'image',
93-
'originalContentUrl' => $data['originalContentUrl'],
94-
'previewImageUrl' => $data['previewImageUrl'] ?? null
95-
]);
96-
}
85+
$imageRequiredFields,
86+
[$this, 'transformImage']
9787
);
9888
$messages = array_merge($messages, $images);
9989
}
10090

10191
if (!empty($details->sendAudio) && !empty($details->audio_field_map)) {
102-
error_log(print_r($details->audio_field_map, true));
10392
$audioFields = array_filter($details->audio_field_map, function ($field) {
10493
return $field->fieldType === 'audio';
10594
});
106-
if (!empty($audioFields)) {
95+
if (\count($audioFields) > 0) {
96+
$audioRequiredFields = ['originalContentUrl'];
10797
$audios = $this->processGrouped(
10898
$values,
10999
$audioFields,
110-
['originalContentUrl'],
111-
function ($data) {
112-
if (empty($data['duration'])) {
113-
return;
114-
}
115-
116-
return [
117-
'type' => 'audio',
118-
'originalContentUrl' => $data['originalContentUrl'],
119-
'duration' => $data['duration']
120-
];
121-
}
100+
$audioRequiredFields,
101+
[$this, 'transformAudio']
122102
);
123103
$audios = array_filter($audios);
124104
$messages = array_merge($messages, $audios);
125105
}
126106
}
127107

128108
if (!empty($details->sendVideo) && !empty($details->video_field_map)) {
109+
$videoRequiredFields = ['originalContentUrl'];
129110
$videos = $this->processGrouped(
130111
$values,
131112
$details->video_field_map,
132-
['originalContentUrl'],
133-
function ($data) {
134-
return array_filter([
135-
'type' => 'video',
136-
'originalContentUrl' => $data['originalContentUrl'],
137-
'previewImageUrl' => $data['previewImageUrl'] ?? null
138-
]);
139-
}
113+
$videoRequiredFields,
114+
[$this, 'transformVideo']
140115
);
141116
$messages = array_merge($messages, $videos);
142117
}
143118

144119
if (!empty($details->sendLocation) && !empty($details->location_field_map)) {
120+
$locationRequiredFields = ['title', 'address', 'latitude', 'longitude'];
145121
$locations = $this->processGrouped(
146122
$values,
147123
$details->location_field_map,
148-
['title', 'address', 'latitude', 'longitude'],
149-
function ($data) {
150-
return [
151-
'type' => 'location',
152-
'title' => $data['title'],
153-
'address' => $data['address'],
154-
'latitude' => (float) $data['latitude'],
155-
'longitude' => (float) $data['longitude'],
156-
];
157-
}
124+
$locationRequiredFields,
125+
[$this, 'transformLocation']
158126
);
159127
$messages = array_merge($messages, $locations);
160128
}
@@ -185,10 +153,10 @@ private function buildTextMessage($details, $values): ?array
185153

186154
if (!empty($details->sendEmojis) && !empty($details->emojis_field_map)) {
187155
$emojis = [];
188-
$groups = $this->groupByGroupId($details->emojis_field_map);
156+
$groups = $this->organizeFieldsByGroup($details->emojis_field_map);
189157
foreach ($groups as $groupId => $groupFields) {
190158
$emoji = $this->mapFields($values, $groupFields);
191-
if (!empty($emoji['emojis_id']) && !empty($emoji['product_id']) && (isset($emoji['index']) && $emoji['index'] !== '')) {
159+
if (isset($emoji['emojis_id'], $emoji['product_id']) && !empty($emoji['index'])) {
192160
$emojis[] = [
193161
'index' => (int) $emoji['index'],
194162
'productId' => $emoji['product_id'],
@@ -204,7 +172,7 @@ private function buildTextMessage($details, $values): ?array
204172
return $message;
205173
}
206174

207-
private function groupByGroupId($fieldMap): array
175+
private function organizeFieldsByGroup(array $fieldMap): array
208176
{
209177
$grouped = [];
210178
foreach ($fieldMap as $field) {
@@ -218,10 +186,10 @@ private function groupByGroupId($fieldMap): array
218186
return $grouped;
219187
}
220188

221-
private function processGrouped($values, $fieldMap, array $requiredKeys, callable $builder): array
189+
private function processGrouped(array $values, array $fieldMap, array $requiredKeys, callable $builder): array
222190
{
223191
$results = [];
224-
$groups = $this->groupByGroupId($fieldMap);
192+
$groups = $this->organizeFieldsByGroup($fieldMap);
225193
foreach ($groups as $groupId => $groupFields) {
226194
$data = $this->mapFields($values, $groupFields);
227195

@@ -242,7 +210,7 @@ private function processGrouped($values, $fieldMap, array $requiredKeys, callabl
242210
return $results;
243211
}
244212

245-
private function mapFields($data, $fieldMap): array
213+
private function mapFields(array $data, array $fieldMap): array
246214
{
247215
$result = [];
248216

@@ -294,4 +262,55 @@ private function sendBroadcastMessage($data)
294262

295263
return static::handleFilterResponse($response);
296264
}
265+
266+
private function transformSticker(array $data): array
267+
{
268+
return [
269+
'type' => 'sticker',
270+
'packageId' => $data['package_id'],
271+
'stickerId' => $data['sticker_id'],
272+
];
273+
}
274+
275+
private function transformImage(array $data): array
276+
{
277+
return array_filter([
278+
'type' => 'image',
279+
'originalContentUrl' => $data['originalContentUrl'],
280+
'previewImageUrl' => $data['previewImageUrl'] ?? null
281+
]);
282+
}
283+
284+
private function transformAudio(array $data): ?array
285+
{
286+
if (empty($data['duration'])) {
287+
return null;
288+
}
289+
290+
return [
291+
'type' => 'audio',
292+
'originalContentUrl' => $data['originalContentUrl'],
293+
'duration' => $data['duration']
294+
];
295+
}
296+
297+
private function transformVideo(array $data): array
298+
{
299+
return array_filter([
300+
'type' => 'video',
301+
'originalContentUrl' => $data['originalContentUrl'],
302+
'previewImageUrl' => $data['previewImageUrl'] ?? null
303+
]);
304+
}
305+
306+
private function transformLocation(array $data): array
307+
{
308+
return [
309+
'type' => 'location',
310+
'title' => $data['title'],
311+
'address' => $data['address'],
312+
'latitude' => (float) $data['latitude'],
313+
'longitude' => (float) $data['longitude'],
314+
];
315+
}
297316
}

0 commit comments

Comments
 (0)