Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
repp_backend
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julieta Dubra Raimunde
repp_backend
Commits
88b1428e
Commit
88b1428e
authored
3 years ago
by
Renzo Beux
Browse files
Options
Downloads
Plain Diff
Merge branch 'feature/faqs' into develop
parents
83672f17
be9076d7
No related branches found
No related tags found
No related merge requests found
Pipeline
#16228
passed with stage
in 1 minute and 48 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Models/FAQ.ts
+0
-1
0 additions, 1 deletion
src/Models/FAQ.ts
src/Services/FAQService.ts
+62
-11
62 additions, 11 deletions
src/Services/FAQService.ts
with
62 additions
and
12 deletions
src/Models/FAQ.ts
+
0
−
1
View file @
88b1428e
...
...
@@ -18,7 +18,6 @@ FAQ.init({
},
position
:
{
type
:
DataTypes
.
INTEGER
,
unique
:
true
,
},
createdBy
:
{
type
:
DataTypes
.
INTEGER
,
...
...
This diff is collapsed.
Click to expand it.
src/Services/FAQService.ts
+
62
−
11
View file @
88b1428e
...
...
@@ -10,34 +10,76 @@ const list = (): Promise<FAQ[]> => FAQ.findAll({
order
:
[
'
position
'
],
});
const
create
=
(
createDto
:
FAQDTO
):
Promise
<
FAQ
>
=>
FAQ
.
create
(
createDto
);
const
correctFAQs
=
async
(
faqs
:
FAQ
[]):
Promise
<
void
>
=>
new
Promise
((
resolve
,
reject
)
=>
{
const
{
length
}
=
faqs
;
const
sorted
:
FAQ
[]
=
faqs
.
sort
((
faqA
:
FAQ
,
faqB
:
FAQ
)
=>
Number
(
faqA
.
get
(
'
position
'
))
-
Number
(
faqB
.
get
(
'
position
'
)));
const
promises
:
Promise
<
FAQ
>
[]
=
[];
for
(
let
index
=
0
;
index
<
length
;
index
+=
1
)
{
const
faq
:
FAQ
=
sorted
[
index
];
promises
.
push
(
faq
.
update
({
position
:
index
+
1
,
}));
}
return
Promise
.
all
(
promises
)
.
then
(()
=>
resolve
())
.
catch
(()
=>
reject
());
});
const
create
=
async
(
createDto
:
FAQDTO
):
Promise
<
FAQ
>
=>
{
const
faqs
:
FAQ
[]
=
await
FAQ
.
findAll
({
where
:
{
deletedAt
:
null
,
},
order
:
[
'
position
'
],
});
const
newFaq
:
FAQ
=
await
FAQ
.
create
(
createDto
);
if
(
faqs
.
length
+
1
===
Number
(
newFaq
.
get
(
'
position
'
)))
{
newFaq
.
set
(
'
position
'
,
Number
(
newFaq
.
get
(
'
position
'
))
+
0.5
);
}
else
{
newFaq
.
set
(
'
position
'
,
Number
(
newFaq
.
get
(
'
position
'
))
-
0.5
);
}
faqs
.
push
(
newFaq
);
await
correctFAQs
(
faqs
);
return
newFaq
;
};
const
update
=
async
(
id
:
number
,
createDto
:
FAQDTO
):
Promise
<
FAQ
|
null
>
=>
{
const
faq
:
FAQ
|
null
=
await
FAQ
.
findOne
({
const
toUpdate
:
FAQ
|
null
=
await
FAQ
.
findOne
({
where
:
{
id
,
deletedAt
:
null
,
},
});
if
(
!
faq
)
{
if
(
!
toUpdate
)
{
return
null
;
}
const
{
question
,
answer
,
position
}
=
createDto
;
const
positionFaq
:
FAQ
|
null
=
await
FAQ
.
findOne
({
await
toUpdate
.
update
({
question
,
answer
,
position
,
});
const
faqs
:
FAQ
[]
=
await
FAQ
.
findAll
({
where
:
{
id
:
{
[
Op
.
n
e
]:
id
,
[
Op
.
n
ot
]:
Number
(
toUpdate
.
get
(
'
id
'
))
,
},
position
,
deletedAt
:
null
,
},
order
:
[
'
position
'
],
});
if
(
positionFaq
)
{
return
null
;
if
(
faqs
.
length
+
1
===
Number
(
toUpdate
.
get
(
'
position
'
)))
{
toUpdate
.
set
(
'
position
'
,
Number
(
toUpdate
.
get
(
'
position
'
))
+
0.5
);
}
else
{
toUpdate
.
set
(
'
position
'
,
Number
(
toUpdate
.
get
(
'
position
'
))
-
0.5
);
}
return
faq
.
update
({
question
,
answer
,
position
,
});
faqs
.
push
(
toUpdate
);
await
correctFAQs
(
faqs
);
return
toUpdate
;
};
const
deleteFAQ
=
async
(
id
:
number
):
Promise
<
boolean
>
=>
{
...
...
@@ -53,6 +95,15 @@ const deleteFAQ = async (id: number): Promise<boolean> => {
await
faq
.
update
({
deletedAt
:
new
Date
(),
});
const
faqs
:
FAQ
[]
=
await
FAQ
.
findAll
({
where
:
{
deletedAt
:
null
,
},
order
:
[
'
position
'
],
});
await
correctFAQs
(
faqs
);
return
true
;
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment